我正在使用Python 2.5并试图在我的程序中使用自定义的excepthook
。在主线程中它工作得很好。但是在线程模块启动的线程中,调用通常的excepthook
。'sys.excepthook'和线程
以下是显示问题的示例。取消注释可显示所需的行为。
import threading, sys
def myexcepthook(type, value, tb):
print 'myexcepthook'
class A(threading.Thread, object):
def __init__(self):
threading.Thread.__init__(self, verbose=True)
# raise Exception('in main')
self.start()
def run(self):
print 'A'
raise Exception('in thread')
if __name__ == "__main__":
sys.excepthook = myexcepthook
A()
那么,我怎样才能在一个线程中使用我自己的excepthook
?
感谢 - 第三个解决方法完美地工作! – Sebastian 2009-10-29 12:42:11