Using Pool map with a method of a class and a list
Luca Cerone
luca.cerone at gmail.com
Wed Aug 7 06:10:51 EDT 2013
> > doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 3 for my application).
>
> Are you using Windows? Over here on 3.3 on Linux it does. Not on 2.7 though.
No I am using Ubuntu (12.04, 64 bit).. maybe things changed from 3.2 to 3.3?
> from multiprocessing import Pool
>
> from functools import partial
>
>
>
> class A(object):
>
> def __init__(self,x):
>
> self.value = x
>
> def fun(self,x):
>
> return self.value**x
>
>
>
> def _getattr_proxy_partialable(instance, name, arg):
>
> return getattr(instance, name)(arg)
>
>
>
> def getattr_proxy(instance, name):
>
> """
>
> A version of getattr that returns a proxy function that can
>
> be pickled. Only function calls will work on the proxy.
>
> """
>
> return partial(_getattr_proxy_partialable, instance, name)
>
>
>
> l = range(100)
>
> p = Pool(4)
>
> op = p.map(getattr_proxy(A(3), "fun"), l)
>
> print(op)
I can't try it now, I'll let you know later if it works!
(Though just by reading I can't really understand what the code does).
Thanks for the help,
Luca
More information about the Python-list
mailing list