The multiprocessing module modifies the global copyreg.dispatch_table to
extend pickling to several built-in types (mostly callable types). In my
humble opinion, this is unacceptable for a standard library module.
Here is an example of a behaviour change made by multiprocessing:
>>> import pickle
>>> pickle.dumps(str.isalpha)
Traceback (most recent call last):
...
_pickle.PicklingError: Can't pickle <class 'method_descriptor'>:
attribute lookup builtins.method_descriptor failed
>>> import multiprocessing.util
>>> pickle.dumps(str.isalpha)
b'\x80\x03cbuiltins\ngetattr\nq\x00cbuiltins\nstr\nq\x01X\x07\x00\x00\x00isalphaq\x02\x86q\x03Rq\x04.'
There was some discussion in issue 558238 about allowing methods to be
pickled. However, no consensus was reached.
In addition, there is a bug in the method pickling support added by
multiprocessing in Python 3.0:
Python 2.6b1+ (trunk:64899:64900, Jul 13 2008, 13:33:02)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import multiprocessing.util, pickle
>>> class A(object):
... def foo(self): pass
...
>>> pickle.dumps(A().foo, 2)
'\x80\x02c__builtin__\ngetattr\nq\x00c__main__\nA\nq\x01)\x81q\x02}q\x03bU\x03fooq\x04\x86q\x05Rq\x06.'
>>> pickle.dumps(A.foo, 2)
'\x80\x02c__builtin__\ngetattr\nq\x00c__main__\nA\nq\x01U\x03fooq\x02\x86q\x03Rq\x04.'
Python 3.0b1+ (py3k:64876M, Jul 11 2008, 12:20:51)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import multiprocessing.util, pickle
>>> class A(object):
... def foo(self): pass
...
>>> pickle.dumps(A().foo, 2)
Traceback (most recent call last):
...
pickle.PicklingError: Can't pickle <class 'method'>: it's not found as
builtins.method
>>> pickle.dumps(A.foo, 2)
Traceback (most recent call last):
...
pickle.PicklingError: Can't pickle <function foo at 0xb7b1392c>: it's
not found as __main__.foo
A better solution for the interprocess communication needs of
multiprocessing would be to define custom subclass of Pickler with
extended support for built-in types. If needed, I am willing to extend
the _pickle module in Python 3.0 to support modification to its
"dispatch table", like the undocumented dispatch_table attribute in
pickle.Pickler.
|