为什么我不能在 Python 中腌制错误的回溯? [英] Why can't I pickle an error's Traceback in Python?

查看:49
本文介绍了为什么我不能在 Python 中腌制错误的回溯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了解决方法,但仍然想知道答案.

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

推荐答案

回溯保存对当前线程上调用的每个函数/方法的堆栈帧的引用,从最顶层的帧到错误被提出.每个堆栈帧还保存对调用堆栈中的每个函数时有效的局部和全局变量的引用.

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.

由于 pickle 无法知道要序列化什么和忽略什么,如果您能够对回溯进行pickle,那么您最终会pickle 整个应用程序状态的移动快照:当pickle 运行时,其他线程可能会正在修改共享变量的值.

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.

一种解决方案是创建一个picklable对象来遍历回溯并仅提取您需要保存的信息.

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

这篇关于为什么我不能在 Python 中腌制错误的回溯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆