Why can't I pickle an error's Traceback in Python?

后端 未结 3 894
猫巷女王i
猫巷女王i 2021-02-18 16:36

I\'ve since found a work around, but still want to know the answer.

3条回答
  •  梦如初夏
    2021-02-18 17:16

    The traceback holds references to the stack frames of each function/method that was called on the current thread, from the topmost-frame on down to the point where the error was raised. Each stack frame also holds references to the local and global variables in effect at the time each function in the stack was called.

    Since there is no way for pickle to know what to serialize and what to ignore, if you were able to pickle a traceback you'd end up pickling a moving snapshot of the entire application state: as pickle runs, other threads may be modifying the values of shared variables.

    One solution is to create a picklable object to walk the traceback and extract only the information you need to save.

提交回复
热议问题