join是等待线程结束,

至于一个线程或是两个线程出错,要怎么重启,

如果线程出错是异常,可以这样做

class ExceptionThread(threading.Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None):
"""
Redirect exceptions of thread to an exception handler.
"""
threading.Thread.__init__(self, group, target, anme, args, kwargs, verbose)
if kwargs is None:
kwargs = {}
self._target = target
self._args = args
self._kwargs = kwargs
self._exc = None
def run(self):
try:
if self._target:
except BaseException as e:
import sys
self._exc = sys.exc_info()
finally:
#Avoid a refcycle if the thread is running a function with
#an argument that has a member that points to the thread.
del self._target, self._args, self._kwargs
def join(self):
threading.Thread.join(self)
if self._exc:
msg = "Thread '%s' threw an exception: %s" % (self.getName(), self._exc[1])
new_exc = Exception(msg)
raise new_exc.__class__, new_exc, self._exc[2]
t = ExceptionThread(target=my_func, name='my_thread', args=(arg1, arg2, ))
t.start()
try:
t.join()
except:
print 'Caught an exception'

参考

join(timeout=None)

Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.