是否可以使用通用异常处理程序捕获Python应用程序的所有线程的异常?
考虑以下示例。我想从主线程中捕获CTRL+C
,但有时它会被一个不终止其他线程的工作线程捕获。这是这种情况下的输出:
^CTraceback (most recent call last):
File "thread-example.py", line 50, in <module>
main()
File "thread-example.py", line 30, in main
t.join(1)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 675, in join
self.__block.wait(delay)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 263, in wait
_sleep(delay)
KeyboardInterrupt
这是测试应用程序。
#!/usr/bin/python
import os
import sys
import threading
import time
import subprocess
import signal
class Worker(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.kill_received = False
self.daemon = True
self.name = name
def run(self):
while not self.kill_received:
time.sleep(1)
print self.name
def main():
threads = []
for i in xrange(10):
t = Worker('worker-%d' % i)
threads.append(t)
t.start()
t.join(1)
while len(threads) > 0:
try:
threads = [t for t in threads if t is not None and t.isAlive()]
time.sleep(1)
except KeyboardInterrupt:
print "Ctrl-c received! Sending kill to threads..."
for t in threads:
t.kill_received = True
# wait for threads to finish gracefully, then kill them anyway.
# since all threads are daemons, they should finish once the main
# loop terminates
for i in xrange(5):
print '%i ...' % (5-i)
time.sleep(1)
print 'Exit!'
os._exit(1)
if __name__ == '__main__':
main()
答案 0 :(得分:1)
Ctrl-C本身不是一个例外,而是一个信号。您可以做的是设置除“main”之外的所有衍生线程以忽略它,如下所示:
import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)