我用python写了一个小线程示例。我面临的问题是,当线程内部出现异常时,该线程会继续运行并且不会退出。我有以下代码:
class Producer (threading.Thread):
def __init__(self, threadId):
threading.Thread.__init__(self)
self.threadId = threadId
self.killReceived = False
def produce(self):
while 1:
if self.killReceived == True:
print self.threadId+"inside kill section"
return False
print "running"
time.sleep(1)
raise Exception('boo')
def run(self):
try:
self.produce()
except Exception as e:
ThreadManager.getInstance().shutdown(self.threadId)
def stop(self):
self.killReceived = True
class ThreadManager:
_instance = None
@staticmethod
def getInstance():
if ThreadManager._instance == None:
ThreadManager._instance = ThreadManager()
return ThreadManager._instance
def __init__(self):
''' some initializations '''
def shutdown(self, threadId):
while threading.active_count() > 1:
for thread in threading.enumerate():
if type(thread) != threading._MainThread: #never kill main thread directly
thread.stop()
#print thread.threadId+" is alive? "+str(thread.isAlive())
当我在生产者内部引发异常时,它被捕获并触发 ThreadManager 的关闭方法,该方法又调用除主线程之外的所有正在运行的线程的 stop() 方法。消费者使用此策略退出,但生产者挂起。如果我运行isAlive
方法,我会看到生产者线程仍在运行,但是它的运行方法不再运行。因为它不再打印。由于 run() 内部running
的方法中出现异常produce
,所以线程应该自动完成。但事实并非如此。那么制作人具体在哪里呢?发生异常时如何使其停止?