我想保存和加载由multiprocessing. manager生成的字典变量。
with Manager() as manager:
score = manager.dict()
entropy = manager.dict()
score['a'] = 1
score['b'] = 2
f_score = open("tttttttt.pkl", "wb")
pickle.dump(dict(score), f_score)
with open('tttttttt.pkl', 'rb') as f:
score_dic = pickle.load(f)
但是,我得到了这个错误。
Traceback (most recent call last):
File "/home/ukjo/anaconda3/envs/spyder/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-5-ccd52887eacf>", line 2, in <module>
score_dic = pickle.load(f)
EOFError: Ran out of input
您小心地使用了with
上下文处理程序来确保您的输入文件f
将被关闭,这对于输入文件来说确实不是那么必要(尽管释放资源肯定是正确的事情),但是忽略了对pickle输出文件执行此操作,其中关闭文件以确保所有数据都被清除是绝对必要的。
修改你的代码:
with open("tttttttt.pkl", "wb") as f_score:
pickle.dump(dict(score), f_score)