[issue9276] pickle should support methods

Amaury Forgeot d'Arc report at bugs.python.org
Tue Mar 1 22:29:15 CET 2011


Amaury Forgeot d'Arc <amauryfa at gmail.com> added the comment:

> Being able to pickle unbound methods is important. In my project I have 
> objects that refer to unbound methods. Now these objects are
> unpickleable. I can't save them to disk and I can't use the
> multiprocessing module on them. That's a big problem.

The multiprocessing module *can* pickle bound and unbound methods (see below), but only with the multiprocessing.Process class. It does not work with Pool.map(), for example.  The reason is that Process uses the special ForkingPickler that has special code to handle methods.  Pool.map could be fixed IMO.

Is "ForkingPickler" enough for your needs?



==== mod.py ============
class C:
    def foo(self):
        print("CALLED")
==== main.py ===========
from mod import C
if __name__ == '__main__':
    from multiprocessing import Process
    p = Process(target=C().foo)
    p.start(); p.join()
    p = Process(target=C.foo, args=(C(),))
    p.start(); p.join()

----------
nosy: +amaury.forgeotdarc

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9276>
_______________________________________


More information about the Python-bugs-list mailing list