我正在使用 Python 2.5 并尝试在程序中使用自定义的 excepthook
。在主线程中,它完美地工作。但在使用 threading 模块启动的线程中,通常的 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
?
threading.excepthook
和sys.excepthook
。这个答案现在应该得到更高的评价。 - Nilan Saha