Skip to content

Instantly share code, notes, and snippets.

@ov7a
Created August 26, 2013 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ov7a/6339051 to your computer and use it in GitHub Desktop.
Save ov7a/6339051 to your computer and use it in GitHub Desktop.
Python global exception hook
def log_uncaught_exceptions(ex_cls, ex, tb):
logging.critical(''.join(traceback.format_tb(tb)))
logging.critical('{0}: {1}'.format(ex_cls, ex))
os._exit(1)
sys.excepthook = log_uncaught_exceptions
def install_thread_excepthook():
"""
Workaround for sys.excepthook thread bug
(https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1230540&group_id=5470).
Call once from __main__ before creating any threads.
If using psyco, call psycho.cannotcompile(threading.Thread.run)
since this replaces a new-style class method.
"""
run_old = threading.Thread.run
def run(*args, **kwargs):
try:
run_old(*args, **kwargs)
except:
sys.excepthook(*sys.exc_info())
threading.Thread.run = run
install_thread_excepthook()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment