Using Pool map with a method of a class and a list

Joshua Landau joshua at landau.ws
Wed Aug 7 11:52:45 EDT 2013


On 7 August 2013 15:46, Peter Otten <__peter__ at web.de> wrote:
> import copy_reg
> import multiprocessing
> import new

"new" is deprecated from 2.6+; use types.MethodType instead of
new.instancemethod.

> def make_instancemethod(inst, methodname):
>     return getattr(inst, methodname)

This is just getattr -- you can replace the two uses of
make_instancemethod with getattr and delete this ;).

> def pickle_instancemethod(method):
>     return make_instancemethod, (method.im_self, method.im_func.__name__)
>
> copy_reg.pickle(
>     new.instancemethod, pickle_instancemethod, make_instancemethod)
>
> class A(object):
>    def __init__(self, a):
>        self.a = a
>    def fun(self, b):
>        return self.a**b
>
> if __name__ == "__main__":
>     items = range(10)
>     pool = multiprocessing.Pool(4)
>     print pool.map(A(3).fun, items)

Well that was easy. The Stackoverflow link made that look *hard*. -1
to my hack, +1 to this.

You can do this in one statement:

copy_reg.pickle(
    types.MethodType,
    lambda method: (getattr, (method.im_self, method.im_func.__name__)),
    getattr
)



More information about the Python-list mailing list