2009-10-29 42 views
13

我正在使用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

回答

9

它看起來像有一個相關的錯誤報告here與解決方法。建議的hack基本上是在try/catch中運行,然後調用sys.excepthook(*sys.exc_info())

+1

感謝 - 第三個解決方法完美地工作! – Sebastian 2009-10-29 12:42:11

8

看起來像這個bug仍然存在於(至少)3.4中,並且Nadia Alramli鏈接的討論中的一個解決方法似乎可以在Python中工作3.4也是。

爲了方便和文檔的緣故,我會在這裏發佈代碼(在我看來)最好的解決方法。我稍微更新了編碼風格和評論,以使它更多PEP8和Pythonic。

import sys 
import threading 

def setup_thread_excepthook(): 
    """ 
    Workaround for `sys.excepthook` thread bug from: 
    http://bugs.python.org/issue1230540 

    Call once from the main thread before creating any threads. 
    """ 

    init_original = threading.Thread.__init__ 

    def init(self, *args, **kwargs): 

     init_original(self, *args, **kwargs) 
     run_original = self.run 

     def run_with_except_hook(*args2, **kwargs2): 
      try: 
       run_original(*args2, **kwargs2) 
      except Exception: 
       sys.excepthook(*sys.exc_info()) 

     self.run = run_with_except_hook 

    threading.Thread.__init__ = init