How to avoid pickling errors when sharing objects between threads?

此生再无相见时 提交于 2019-12-04 16:43:01
ivan_pozdeev

As others have mentioned, you cannot pickle "volatile" entities (threads, connections, synchonization primitives etc.) because they don't make sense as persistent data.

Looks like what you're trying to do is be saving session variables for later continuation of said session. For that task, there's nothing you can do to save objects that, well, cannot be saved due to their nature.

The simplest solution is just to ignore them. Replacing them with some "stubs" that would yield an error anytime you do anything with them makes little sense, as a missing variable yields an error anyway (it won't if it was masking a global variable but that is a questionable practice in itself).

Alternatively, you can set up these objects anew when restoring a session. But such a logic is by necessity task-specific.

Finally, e.g. IPython already has session saving/restoration logic, so you probably don't need to reinvent the wheel at all.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!