python 中的 Pickling 方法描述符对象
Pickling method descriptor objects in python
我正在尝试泡菜 method_descriptor
。
用 pickle
或 cloudpickle
酸洗失败:
Python 2.7.10 |Continuum Analytics, Inc.| (default, Oct 19 2015, 18:04:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import pickle, cloudpickle
>>> pickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 1374, in dumps
Pickler(file, protocol).dump(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: cannot pickle method_descriptor objects
>>> cloudpickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 602, in dumps
cp.dump(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 111, in dump
raise pickle.PicklingError(msg)
pickle.PicklingError: Could not pickle object as excessively deep recursion required.
导入 dill
以某种方式使 pickle
工作,如下所示:
>>> import dill
>>> pickle.dumps(set.union)
'cdill.dill\n_getattr\np0\n(c__builtin__\nset\np1\nS\'union\'\np2\nS"<method \'union\' of \'set\' objects>"\np3\ntp4\nRp5\n.'
>>> f = pickle.loads(pickle.dumps(set.union))
>>> set.union(set([1,2]), set([3]))
set([1, 2, 3])
>>> f(set([1,2]), set([3]))
set([1, 2, 3])
cloudpickle
中的问题即使在 dill
导入之后仍然存在:
>>> cloudpickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 602, in dumps
cp.dump(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 111, in dump
raise pickle.PicklingError(msg)
pickle.PicklingError: Could not pickle object as excessively deep recursion required.
在我的应用程序中,我依靠 cloudpickle
来处理具有全局变量的函数。所以我的问题是,如何让 cloudpickle
为 Python 2.7 中的 method_descriptor
对象工作?
编辑:我注意到 Python 3.3 中出现了同样的问题,但 Python 3.5 中没有。
我是 dill
的作者。当您执行 import dill
时,它会将 dill
中的序列化注册表注入 pickle
(基本上,将 dill
中的所有 copy_reg
类型的知识放入 pickle
注册表)。
>>> import pickle
>>> pickle.Pickler.dispatch
{<type 'function'>: <function save_global at 0x105d0c7d0>, <type 'dict'>: <function save_dict at 0x105d0c668>, <type 'int'>: <function save_int at 0x105d0c230>, <type 'long'>: <function save_long at 0x105d0c2a8>, <type 'list'>: <function save_list at 0x105d0c578>, <type 'str'>: <function save_string at 0x105d0c398>, <type 'unicode'>: <function save_unicode at 0x105d0c410>, <type 'instance'>: <function save_inst at 0x105d0c758>, <type 'type'>: <function save_global at 0x105d0c7d0>, <type 'NoneType'>: <function save_none at 0x105d0c140>, <type 'bool'>: <function save_bool at 0x105d0c1b8>, <type 'tuple'>: <function save_tuple at 0x105d0c488>, <type 'float'>: <function save_float at 0x105d0c320>, <type 'classobj'>: <function save_global at 0x105d0c7d0>, <type 'builtin_function_or_method'>: <function save_global at 0x105d0c7d0>}
>>> import dill
>>> pickle.Pickler.dispatch
{<class '_pyio.BufferedReader'>: <function save_file at 0x106c8b848>, <class '_pyio.TextIOWrapper'>: <function save_file at 0x106c8b848>, <type 'operator.itemgetter'>: <function save_itemgetter at 0x106c8b578>, <type 'weakproxy'>: <function save_weakproxy at 0x106c8c050>, <type 'NoneType'>: <function save_none at 0x105d0c140>, <type 'str'>: <function save_string at 0x105d0c398>, <type 'file'>: <function save_file at 0x106c8b8c0>, <type 'classmethod'>: <function save_classmethod at 0x106c8c230>, <type 'float'>: <function save_float at 0x105d0c320>, <type 'instancemethod'>: <function save_instancemethod0 at 0x106c8ba28>, <type 'cell'>: <function save_cell at 0x106c8bb18>, <type 'member_descriptor'>: <function save_wrapper_descriptor at 0x106c8bc08>, <type 'slice'>: <function save_slice at 0x106c8bc80>, <type 'dict'>: <function save_module_dict at 0x106c8b410>, <type 'long'>: <function save_long at 0x105d0c2a8>, <type 'code'>: <function save_code at 0x106c8b320>, <type 'type'>: <function save_type at 0x106c8c0c8>, <type 'xrange'>: <function save_singleton at 0x106c8bde8>, <type 'builtin_function_or_method'>: <function save_builtin_method at 0x106c8b9b0>, <type 'classobj'>: <function save_classobj at 0x106c8b488>, <type 'weakref'>: <function save_weakref at 0x106c8bed8>, <type 'getset_descriptor'>: <function save_wrapper_descriptor at 0x106c8bc08>, <type 'weakcallableproxy'>: <function save_weakproxy at 0x106c8c050>, <class '_pyio.BufferedRandom'>: <function save_file at 0x106c8b848>, <type 'int'>: <function save_int at 0x105d0c230>, <type 'list'>: <function save_list at 0x105d0c578>, <type 'functools.partial'>: <function save_functor at 0x106c8b7d0>, <type 'bool'>: <function save_bool at 0x105d0c1b8>, <type 'function'>: <function save_function at 0x106c8b398>, <type 'thread.lock'>: <function save_lock at 0x106c8b500>, <type 'super'>: <function save_functor at 0x106c8b938>, <type 'staticmethod'>: <function save_classmethod at 0x106c8c230>, <type 'module'>: <function save_module at 0x106c8bf50>, <type 'method_descriptor'>: <function save_wrapper_descriptor at 0x106c8bc08>, <type 'operator.attrgetter'>: <function save_attrgetter at 0x106c8b5f0>, <type 'wrapper_descriptor'>: <function save_wrapper_descriptor at 0x106c8bc08>, <type 'numpy.ufunc'>: <function save_numpy_ufunc at 0x106c8bcf8>, <type 'method-wrapper'>: <function save_instancemethod at 0x106c8baa0>, <type 'instance'>: <function save_inst at 0x105d0c758>, <type 'cStringIO.StringI'>: <function save_stringi at 0x106c8b6e0>, <type 'unicode'>: <function save_unicode at 0x105d0c410>, <class '_pyio.BufferedWriter'>: <function save_file at 0x106c8b848>, <type 'property'>: <function save_property at 0x106c8c140>, <type 'ellipsis'>: <function save_singleton at 0x106c8bde8>, <type 'tuple'>: <function save_tuple at 0x105d0c488>, <type 'cStringIO.StringO'>: <function save_stringo at 0x106c8b758>, <type 'NotImplementedType'>: <function save_singleton at 0x106c8bde8>, <type 'dictproxy'>: <function save_dictproxy at 0x106c8bb90>}
cloudpickle
与 dill
具有(略微)不同的 pickling 函数,如果您使用 cloudpickle
,它会将自己的序列化函数推送到 pickle
注册表中。如果你想让 cloudpickle
为你工作,你也许可以通过 monkeypatch 解决方案......本质上是在你的应用程序中安装一个模块来实现 import dill as cloudpickle
(很好的参考:http://blog.dscpl.com.au/2015/03/safely-applying-monkey-patches-in-python.html)......但是这将在您的应用程序上下文中用 dill
替换 cloudpickle
的全部使用。您也可以按照以下方式尝试使用 monkeypatch:
>>> #first import dill, which populates itself into pickle's dispatch
>>> import dill
>>> import pickle
>>> # save the MethodDescriptorType from dill
>>> MethodDescriptorType = type(type.__dict__['mro'])
>>> MethodDescriptorWrapper = pickle.Pickler.dispatch[MethodDescriptorType]
>>> # cloudpickle does the same, so let it update the dispatch table
>>> import cloudpickle
>>> # now, put the saved MethodDescriptorType back in
>>> pickle.Pickler.dispatch[MethodDescriptorWrapperType] = MethodDescriptorWrapper
请注意,如果您打算直接使用 cloudpickle.dumps
,则必须通过在 cloudpickle.CloudPickler.dispatch
.[=42 上执行上述 monkeypatch 直接重载 cloudpickle
中的注册表=]
我不保证它 会 工作,我也不保证它不会搞砸来自 cloudpickle
的其他对象(基本上,我没有我没试过),但这是用 dill
.
中的包装器替换有问题的 cloudpickle
包装器的潜在途径
如果你想要简短的答案,我会说(至少对于这种情况)使用 dill
。 ;)
编辑 关于 copyreg
:
这是 dill
中的内容:
def _getattr(objclass, name, repr_str):
# hack to grab the reference directly
try:
attr = repr_str.split("'")[3]
return eval(attr+'.__dict__["'+name+'"]')
except:
attr = getattr(objclass,name)
if name == '__dict__':
attr = attr[name]
return attr
用于注册具有较低级别 reduce 函数的函数(直接在 pickler 实例上)。 obj
是要 pickle 的对象。
pickler.save_reduce(_getattr, (obj.__objclass__, obj.__name__, obj.__repr__()), obj=obj)
我相信这会转化为 reduce 方法(直接在 copyreg.pickle
中使用),如下所示:
def _reduce_method_descriptor(obj):
return _getattr, (obj.__objclass__, obj.__name__, obj.__repr__())
经过一番折腾,我想我已经找到了适用于 Python 2.7 和 3.3 的问题的明确答案。请注意,Python 3.5 没有任何问题。
在展示我的发现结果之前,我想感谢 multiprocessing.forking
模块,这是我获得使这项工作的代码要点的地方。
下面以set.union
为例<class 'method_descriptor'>
。
Python 3.5:方法描述符开箱即用
Python 3.5.0 |Continuum Analytics, Inc.| (default, Oct 19 2015, 21:57:25)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dumps(set.union)
b'\x80\x03cbuiltins\ngetattr\nq\x00cbuiltins\nset\nq\x01X\x05\x00\x00\x00unionq\x02\x86q\x03Rq\x04.'
>>> f = pickle.loads(pickle.dumps(set.union))
>>> f({1, 2, 3}, {5})
{1, 2, 3, 5}
>>>
Python 3.3:使用copyreg
为pickle
提供一种与method_descriptor
一起工作的方式
Python 3.3.5 |Continuum Analytics, Inc.| (default, Jun 4 2015, 15:22:11)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class 'method_descriptor'>: attribute lookup builtins.method_descriptor failed
set.union
的类型是method_descriptor
:
>>> type(set.union)
<class 'method_descriptor'>
我们为method_descriptor定义reduce函数,并用copyreg
注册:
>>> def _reduce_method_descriptor(m):
... return getattr, (m.__objclass__, m.__name__)
...
>>> import copyreg
>>> copyreg.pickle(type(set.union), _reduce_method_descriptor)
成功:
>>> pickle.dumps(set.union)
b'\x80\x03cbuiltins\ngetattr\nq\x00cbuiltins\nset\nq\x01X\x05\x00\x00\x00unionq\x02\x86q\x03Rq\x04.'
>>> f = pickle.loads(pickle.dumps(set.union))
>>> f({1, 2, 3}, {5})
{1, 2, 3, 5}
如果我们现在导入 cloudpickle
已注册的酸洗功能仍然有效:
>>> import cloudpickle
>>> cloudpickle.dumps(set.union)
b'\x80\x02c__builtin__\ngetattr\nq\x00c__builtin__\nset\nq\x01X\x05\x00\x00\x00unionq\x02\x86q\x03Rq\x04.'
>>> f = pickle.loads(pickle.dumps(set.union))
>>> f({1, 2, 3}, {5})
{1, 2, 3, 5}
>>>
Python 2.7:使用copy_reg
为pickle
提供一种与method_descriptor
一起工作的方式
在Python2.7中,注册pickle支持函数的模块叫做copy_reg
。
Python 2.7.10 |Continuum Analytics, Inc.| (default, Oct 19 2015, 18:04:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import pickle
>>> pickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 1374, in dumps
Pickler(file, protocol).dump(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle method_descriptor objects
set.union
的类型是method_descriptor
:
>>> type(set.union)
<type 'method_descriptor'>
我们为method_descriptor定义reduce函数,并用copyreg
注册:
>>> def _reduce_method_descriptor(m):
... return getattr, (m.__objclass__, m.__name__)
...
>>> import copy_reg
>>> copy_reg.pickle(type(set.union), _reduce_method_descriptor)
>>> pickle.dumps(set.union)
"c__builtin__\ngetattr\np0\n(c__builtin__\nset\np1\nS'union'\np2\ntp3\nRp4\n."
成功:
>>> f = pickle.loads(pickle.dumps(set.union))
>>> f(set([1, 2, 3]), set([5]))
也适用于 cloudpickle
:
set([1, 2, 3, 5])
>>> import cloudpickle
>>> cloudpickle.dumps(set.union)
'\x80\x02c__builtin__\ngetattr\nq\x00c__builtin__\nset\nq\x01U\x05unionq\x02\x86q\x03Rq\x04.'
>>> f = pickle.loads(cloudpickle.dumps(set.union))
>>> f(set([1, 2, 3]), set([5]))
set([1, 2, 3, 5])
>>>
我正在尝试泡菜 method_descriptor
。
用 pickle
或 cloudpickle
酸洗失败:
Python 2.7.10 |Continuum Analytics, Inc.| (default, Oct 19 2015, 18:04:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import pickle, cloudpickle
>>> pickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 1374, in dumps
Pickler(file, protocol).dump(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: cannot pickle method_descriptor objects
>>> cloudpickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 602, in dumps
cp.dump(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 111, in dump
raise pickle.PicklingError(msg)
pickle.PicklingError: Could not pickle object as excessively deep recursion required.
导入 dill
以某种方式使 pickle
工作,如下所示:
>>> import dill
>>> pickle.dumps(set.union)
'cdill.dill\n_getattr\np0\n(c__builtin__\nset\np1\nS\'union\'\np2\nS"<method \'union\' of \'set\' objects>"\np3\ntp4\nRp5\n.'
>>> f = pickle.loads(pickle.dumps(set.union))
>>> set.union(set([1,2]), set([3]))
set([1, 2, 3])
>>> f(set([1,2]), set([3]))
set([1, 2, 3])
cloudpickle
中的问题即使在 dill
导入之后仍然存在:
>>> cloudpickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 602, in dumps
cp.dump(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 111, in dump
raise pickle.PicklingError(msg)
pickle.PicklingError: Could not pickle object as excessively deep recursion required.
在我的应用程序中,我依靠 cloudpickle
来处理具有全局变量的函数。所以我的问题是,如何让 cloudpickle
为 Python 2.7 中的 method_descriptor
对象工作?
编辑:我注意到 Python 3.3 中出现了同样的问题,但 Python 3.5 中没有。
我是 dill
的作者。当您执行 import dill
时,它会将 dill
中的序列化注册表注入 pickle
(基本上,将 dill
中的所有 copy_reg
类型的知识放入 pickle
注册表)。
>>> import pickle
>>> pickle.Pickler.dispatch
{<type 'function'>: <function save_global at 0x105d0c7d0>, <type 'dict'>: <function save_dict at 0x105d0c668>, <type 'int'>: <function save_int at 0x105d0c230>, <type 'long'>: <function save_long at 0x105d0c2a8>, <type 'list'>: <function save_list at 0x105d0c578>, <type 'str'>: <function save_string at 0x105d0c398>, <type 'unicode'>: <function save_unicode at 0x105d0c410>, <type 'instance'>: <function save_inst at 0x105d0c758>, <type 'type'>: <function save_global at 0x105d0c7d0>, <type 'NoneType'>: <function save_none at 0x105d0c140>, <type 'bool'>: <function save_bool at 0x105d0c1b8>, <type 'tuple'>: <function save_tuple at 0x105d0c488>, <type 'float'>: <function save_float at 0x105d0c320>, <type 'classobj'>: <function save_global at 0x105d0c7d0>, <type 'builtin_function_or_method'>: <function save_global at 0x105d0c7d0>}
>>> import dill
>>> pickle.Pickler.dispatch
{<class '_pyio.BufferedReader'>: <function save_file at 0x106c8b848>, <class '_pyio.TextIOWrapper'>: <function save_file at 0x106c8b848>, <type 'operator.itemgetter'>: <function save_itemgetter at 0x106c8b578>, <type 'weakproxy'>: <function save_weakproxy at 0x106c8c050>, <type 'NoneType'>: <function save_none at 0x105d0c140>, <type 'str'>: <function save_string at 0x105d0c398>, <type 'file'>: <function save_file at 0x106c8b8c0>, <type 'classmethod'>: <function save_classmethod at 0x106c8c230>, <type 'float'>: <function save_float at 0x105d0c320>, <type 'instancemethod'>: <function save_instancemethod0 at 0x106c8ba28>, <type 'cell'>: <function save_cell at 0x106c8bb18>, <type 'member_descriptor'>: <function save_wrapper_descriptor at 0x106c8bc08>, <type 'slice'>: <function save_slice at 0x106c8bc80>, <type 'dict'>: <function save_module_dict at 0x106c8b410>, <type 'long'>: <function save_long at 0x105d0c2a8>, <type 'code'>: <function save_code at 0x106c8b320>, <type 'type'>: <function save_type at 0x106c8c0c8>, <type 'xrange'>: <function save_singleton at 0x106c8bde8>, <type 'builtin_function_or_method'>: <function save_builtin_method at 0x106c8b9b0>, <type 'classobj'>: <function save_classobj at 0x106c8b488>, <type 'weakref'>: <function save_weakref at 0x106c8bed8>, <type 'getset_descriptor'>: <function save_wrapper_descriptor at 0x106c8bc08>, <type 'weakcallableproxy'>: <function save_weakproxy at 0x106c8c050>, <class '_pyio.BufferedRandom'>: <function save_file at 0x106c8b848>, <type 'int'>: <function save_int at 0x105d0c230>, <type 'list'>: <function save_list at 0x105d0c578>, <type 'functools.partial'>: <function save_functor at 0x106c8b7d0>, <type 'bool'>: <function save_bool at 0x105d0c1b8>, <type 'function'>: <function save_function at 0x106c8b398>, <type 'thread.lock'>: <function save_lock at 0x106c8b500>, <type 'super'>: <function save_functor at 0x106c8b938>, <type 'staticmethod'>: <function save_classmethod at 0x106c8c230>, <type 'module'>: <function save_module at 0x106c8bf50>, <type 'method_descriptor'>: <function save_wrapper_descriptor at 0x106c8bc08>, <type 'operator.attrgetter'>: <function save_attrgetter at 0x106c8b5f0>, <type 'wrapper_descriptor'>: <function save_wrapper_descriptor at 0x106c8bc08>, <type 'numpy.ufunc'>: <function save_numpy_ufunc at 0x106c8bcf8>, <type 'method-wrapper'>: <function save_instancemethod at 0x106c8baa0>, <type 'instance'>: <function save_inst at 0x105d0c758>, <type 'cStringIO.StringI'>: <function save_stringi at 0x106c8b6e0>, <type 'unicode'>: <function save_unicode at 0x105d0c410>, <class '_pyio.BufferedWriter'>: <function save_file at 0x106c8b848>, <type 'property'>: <function save_property at 0x106c8c140>, <type 'ellipsis'>: <function save_singleton at 0x106c8bde8>, <type 'tuple'>: <function save_tuple at 0x105d0c488>, <type 'cStringIO.StringO'>: <function save_stringo at 0x106c8b758>, <type 'NotImplementedType'>: <function save_singleton at 0x106c8bde8>, <type 'dictproxy'>: <function save_dictproxy at 0x106c8bb90>}
cloudpickle
与 dill
具有(略微)不同的 pickling 函数,如果您使用 cloudpickle
,它会将自己的序列化函数推送到 pickle
注册表中。如果你想让 cloudpickle
为你工作,你也许可以通过 monkeypatch 解决方案......本质上是在你的应用程序中安装一个模块来实现 import dill as cloudpickle
(很好的参考:http://blog.dscpl.com.au/2015/03/safely-applying-monkey-patches-in-python.html)......但是这将在您的应用程序上下文中用 dill
替换 cloudpickle
的全部使用。您也可以按照以下方式尝试使用 monkeypatch:
>>> #first import dill, which populates itself into pickle's dispatch
>>> import dill
>>> import pickle
>>> # save the MethodDescriptorType from dill
>>> MethodDescriptorType = type(type.__dict__['mro'])
>>> MethodDescriptorWrapper = pickle.Pickler.dispatch[MethodDescriptorType]
>>> # cloudpickle does the same, so let it update the dispatch table
>>> import cloudpickle
>>> # now, put the saved MethodDescriptorType back in
>>> pickle.Pickler.dispatch[MethodDescriptorWrapperType] = MethodDescriptorWrapper
请注意,如果您打算直接使用 cloudpickle.dumps
,则必须通过在 cloudpickle.CloudPickler.dispatch
.[=42 上执行上述 monkeypatch 直接重载 cloudpickle
中的注册表=]
我不保证它 会 工作,我也不保证它不会搞砸来自 cloudpickle
的其他对象(基本上,我没有我没试过),但这是用 dill
.
cloudpickle
包装器的潜在途径
如果你想要简短的答案,我会说(至少对于这种情况)使用 dill
。 ;)
编辑 关于 copyreg
:
这是 dill
中的内容:
def _getattr(objclass, name, repr_str):
# hack to grab the reference directly
try:
attr = repr_str.split("'")[3]
return eval(attr+'.__dict__["'+name+'"]')
except:
attr = getattr(objclass,name)
if name == '__dict__':
attr = attr[name]
return attr
用于注册具有较低级别 reduce 函数的函数(直接在 pickler 实例上)。 obj
是要 pickle 的对象。
pickler.save_reduce(_getattr, (obj.__objclass__, obj.__name__, obj.__repr__()), obj=obj)
我相信这会转化为 reduce 方法(直接在 copyreg.pickle
中使用),如下所示:
def _reduce_method_descriptor(obj):
return _getattr, (obj.__objclass__, obj.__name__, obj.__repr__())
经过一番折腾,我想我已经找到了适用于 Python 2.7 和 3.3 的问题的明确答案。请注意,Python 3.5 没有任何问题。
在展示我的发现结果之前,我想感谢 multiprocessing.forking
模块,这是我获得使这项工作的代码要点的地方。
下面以set.union
为例<class 'method_descriptor'>
。
Python 3.5:方法描述符开箱即用
Python 3.5.0 |Continuum Analytics, Inc.| (default, Oct 19 2015, 21:57:25)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dumps(set.union)
b'\x80\x03cbuiltins\ngetattr\nq\x00cbuiltins\nset\nq\x01X\x05\x00\x00\x00unionq\x02\x86q\x03Rq\x04.'
>>> f = pickle.loads(pickle.dumps(set.union))
>>> f({1, 2, 3}, {5})
{1, 2, 3, 5}
>>>
Python 3.3:使用copyreg
为pickle
提供一种与method_descriptor
一起工作的方式
Python 3.3.5 |Continuum Analytics, Inc.| (default, Jun 4 2015, 15:22:11)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class 'method_descriptor'>: attribute lookup builtins.method_descriptor failed
set.union
的类型是method_descriptor
:
>>> type(set.union)
<class 'method_descriptor'>
我们为method_descriptor定义reduce函数,并用copyreg
注册:
>>> def _reduce_method_descriptor(m):
... return getattr, (m.__objclass__, m.__name__)
...
>>> import copyreg
>>> copyreg.pickle(type(set.union), _reduce_method_descriptor)
成功:
>>> pickle.dumps(set.union)
b'\x80\x03cbuiltins\ngetattr\nq\x00cbuiltins\nset\nq\x01X\x05\x00\x00\x00unionq\x02\x86q\x03Rq\x04.'
>>> f = pickle.loads(pickle.dumps(set.union))
>>> f({1, 2, 3}, {5})
{1, 2, 3, 5}
如果我们现在导入 cloudpickle
已注册的酸洗功能仍然有效:
>>> import cloudpickle
>>> cloudpickle.dumps(set.union)
b'\x80\x02c__builtin__\ngetattr\nq\x00c__builtin__\nset\nq\x01X\x05\x00\x00\x00unionq\x02\x86q\x03Rq\x04.'
>>> f = pickle.loads(pickle.dumps(set.union))
>>> f({1, 2, 3}, {5})
{1, 2, 3, 5}
>>>
Python 2.7:使用copy_reg
为pickle
提供一种与method_descriptor
一起工作的方式
在Python2.7中,注册pickle支持函数的模块叫做copy_reg
。
Python 2.7.10 |Continuum Analytics, Inc.| (default, Oct 19 2015, 18:04:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import pickle
>>> pickle.dumps(set.union)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 1374, in dumps
Pickler(file, protocol).dump(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/home/pmd/anaconda3/envs/python2/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle method_descriptor objects
set.union
的类型是method_descriptor
:
>>> type(set.union)
<type 'method_descriptor'>
我们为method_descriptor定义reduce函数,并用copyreg
注册:
>>> def _reduce_method_descriptor(m):
... return getattr, (m.__objclass__, m.__name__)
...
>>> import copy_reg
>>> copy_reg.pickle(type(set.union), _reduce_method_descriptor)
>>> pickle.dumps(set.union)
"c__builtin__\ngetattr\np0\n(c__builtin__\nset\np1\nS'union'\np2\ntp3\nRp4\n."
成功:
>>> f = pickle.loads(pickle.dumps(set.union))
>>> f(set([1, 2, 3]), set([5]))
也适用于 cloudpickle
:
set([1, 2, 3, 5])
>>> import cloudpickle
>>> cloudpickle.dumps(set.union)
'\x80\x02c__builtin__\ngetattr\nq\x00c__builtin__\nset\nq\x01U\x05unionq\x02\x86q\x03Rq\x04.'
>>> f = pickle.loads(cloudpickle.dumps(set.union))
>>> f(set([1, 2, 3]), set([5]))
set([1, 2, 3, 5])
>>>