问题描述
我的Python3代码中有一个大对象,当尝试用pickle
模块腌制时,会引发以下错误:
I have a large object in my Python3 code which, when tried to be pickled with the pickle
module throws the following error:
TypeError: cannot serialize '_io.BufferedReader' object
但是,dill.dump()
和dill.load()
能够无缝保存和恢复对象.
However, dill.dump()
and dill.load()
are able to save and restore the object seamlessly.
- 是什么原因导致
pickle
模块出现问题? - 现在
dill
保存并重建对象没有任何错误,是否有任何方法可以验证用dill
进行的酸洗和酸洗是否顺利? -
pickle
失败但dill
成功的可能性如何?
- What causes the trouble for the
pickle
module? - Now that
dill
saves and reconstructs the object without any error, is there any way to verify if the pickling and unpickling withdill
went well? - How's it possible that
pickle
fails, butdill
succeeds?
推荐答案
我是dill
的作者.
1)最简单的方法是查看此文件: https ://github.com/uqfoundation/dill/blob/master/dill/_objects.py ,其中列出了pickle
可以序列化的内容,以及dill
可以序列化的内容.
1) Easiest thing to do is look at this file: https://github.com/uqfoundation/dill/blob/master/dill/_objects.py, it lists what pickle
can serialize, and what dill
can serialize.
2)您可以尝试dill.copy
和dill.check
和dill.pickles
来检查不同级别的酸洗和解酸. dill
还包括用于检测和诊断dill.detect
和dill.pointers
中的序列化问题的实用程序.
2) you can try dill.copy
and dill.check
and dill.pickles
to check different levels of pickling and unpickling. dill
also more includes utilities for detecting and diagnosing serialization issues in dill.detect
and dill.pointers
.
3)dill
建立在pickle
的基础上,并通过注册新的序列化函数对其进行了扩充.
3) dill
is built on pickle
, and augments it by registering new serialization functions.
4)dill
包括序列化变体,使用户可以从不同的对象依赖项序列化策略中进行选择(在dill.settings
中)-包括源代码提取和使用dill.source
进行对象重构(以及stdlib 模块).
4) dill
includes serialization variants which enable the user to choose from different object dependency serialization strategies (in dill.settings
) -- including source code extraction and object reconstitution with dill.source
(and extension of the stdlib inspect
module).
这篇关于莳萝与Python的pickle模块有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!