在线程之间共享对象时,如何避免出现酸洗错误? [英] How to avoid pickling errors when sharing objects between threads?
问题描述
我有一个程序,需要在其中将全局变量存储到文件中.我正在使用pickle
模块执行此操作.
I have a program in which I need to store a global variable into a file. I am doing this using the pickle
module.
我还有另一个thread
(来自threading
模块的Daemon = False
),有时会更改全局变量的值.该值也在全局范围内(主程序)被修改.
I have another thread
(Daemon=False
, from threading
module) which sometimes changes the value of the global variable. The value is also modified in global scope(the main program).
我每5秒钟将变量的值转储到.pkl
文件中(使用threading
模块中的另一个thread
).
I am dumping the value of the variable into a .pkl
file every 5 seconds (using another thread
from threading
module).
但是执行dump
方法时发现以下错误:
But I found the following error when dump
method was executed:
TypeError: can't pickle _thread.lock objects
为什么会这样?而我该如何解决?
Why is this happening? And what can I do to fix it?
注意:我已经在multiprocessing
模块中找到了一些类似的答案.但是我需要一个threading
模块的答案.
Note: I have found some similar answers with multiprocessing
module. But I need an answer for threading
module.
代码:
def save_state():
while True:
global variable
lastSession = open('lastSession.pkl', 'wb')
# error occurs on this line
pickle.dump(variable, lastSession)
lastSession.close()
time.sleep(5)
state_thread = threading.Thread(target = save_state)
state_thread.setDaemon(False)
state_thread.start()
# variable is changed outside this function and also in another thread(not state_thread).
推荐答案
就像其他人提到的那样,您不能腌制易失"实体(线程,连接,同步化原语等),因为它们对于持久性数据没有意义.
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.
最后,例如IPython 已经具有会话保存/恢复逻辑,所以您可能没有根本不需要重新发明轮子.
Finally, e.g. IPython already has session saving/restoration logic, so you probably don't need to reinvent the wheel at all.
这篇关于在线程之间共享对象时,如何避免出现酸洗错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!