How to serialize an Exception
Exceptions can not be pickled (by default), you have two options:
Use Python's built in format_exc() and serialize the formatted string.
Use
tblib
With the latter, you can pass wrapped exceptions and also reraise them later.
import tblib.pickling_supporttblib.pickling_support.install()import pickle, sys def inner_0(): raise Exception('fail')def inner_1(): inner_0()def inner_2(): inner_1()try: inner_2()except: s1 = pickle.dumps(sys.exc_info())
You can use exc_info
with traceback
as below:
import tracebackimport systry: raise KeyError('aaa!!!')except Exception as e: exc_info = sys.exc_info() print(''.join(traceback.format_exception(*exc_info)))