如何使用 try 异常块同时 运行 两个函数

How to run two function in the same time with using try exception block

来自 Make 2 functions run at the same time 通过使用线程。它可以同时实现两个功能。是否可以在 try except 块中用线程调用函数?

import threading
from threading import Thread
import time

def queryRepeatedly():
    while True:
        while True:
            try:
                Thread(target = foo).start()
                Thread(target = bar).start()
                print ''
            except:
                continue
            else:
                break

def foo():
      print 'foo'
      time.sleep(1)
def bar():
      print 'bar'
      time.sleep(3)

queryRepeatedly()

我的代码不起作用,我需要 运行 两个函数分别使用 try except 块。我应该怎么办?

我想这可能是您要找的:

import threading
from threading import Thread
import time

def queryRepeatedly():
   Thread(target = foo).start()
   Thread(target = bar).start()

def foo():
    while True:
        try:
            print 'foo'
        except : #catch your error here
           pass # handle your error here
        finally:
           time.sleep(1)
def bar():
    While True:
        try:
           print 'bar'
        except : #catch your error here
           pass # handle your error here
        finally:
           time.sleep(3)

queryRepeatedly()