2009-10-29 14 views
13

Sto usando Python 2.5 e sto provando a usare un auto-definito excepthook nel mio programma. Nel thread principale funziona perfettamente. Ma in un thread avviato con il modulo di threading viene chiamato il solito excepthook.'sys.excepthook' e threading

Ecco un esempio che mostra il problema. Scomporre il commento mostra il comportamento desiderato.

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() 

Così, come posso usare la mia excepthook in un thread?

risposta

9

Sembra che sia stato riscontrato un bug correlato here con soluzioni alternative. Gli hack suggeriti si basano fondamentalmente in un try/catch e chiamano sys.excepthook(*sys.exc_info())

+1

Grazie - la terza soluzione funziona perfettamente! – Sebastian

8

Sembra che questo bug sia ancora presente in (almeno) 3.4 e uno dei workaround nella discussione di Nadia Alramli collegata sembra funzionare in Python 3.4 anche.

Per motivi di praticità e documentazione, inserirò il codice per (a mio parere) il miglior rimedio qui. Ho aggiornato leggermente lo stile di codifica e i commenti per renderlo più PEP8 e 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