我在python中编写了一个小线程示例。我面临的问题是,当线程内有异常时,该线程继续运行,并且不会退出。我有下面的代码:线程不退出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
方法,我发现生产者线程仍在运行,但它的运行方法不再运行。因为它不再打印running
。作为在run()内部的produce
方法的异常冒泡,所以线程应该自动完成。但事实并非如此。那么制作人究竟在哪里呢?发生某种异常时如何停止?
是的,你是正确的循环的事情。通话永远不会结束,所以制作人似乎永远不会退出ThreadPoolExecutor,这是一个很好的建议,我会研究它。感谢您指导我。 58K积分!!!(敬礼) – Shades88