Passing exceptions between threadsLet's consider a typical client-server paradigm, thread 1 performs some work for thread 2, the result is typically a value, but sometimes an exception is raised.
Let's make thread 2 pretend action was performed synchronously.
If an exception is raised, user wants to see full stack, that is frames from thread 1 and thread 2.
# thread 1 def do_thing_quickly(): try: shared.result = ... except Exception: shared.exc_info = sys.exc_info() # thread 2 def do_thing_especially_quickly(): # ask thread 1 to do thing quickly # wait for thread 1 to finish if shared.exc_info: raise shared.exc_info, shared.exc_info, shared.exc_info use(shared.result)
Logging saved exceptions
In : import sys, logging In : try: map(int, "1234ස!") except: exc_info = sys.exc_info() ...: In : logging.error("fracking toasters", exc_info=exc_info) ERROR:root:fracking toasters Traceback (most recent call last): File "<ipython-input-5-ca8ed6e70829>", line 1, in <module> try: map(int, "234a") ValueError: invalid literal for int() with base 10: '\xe0'