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

Joshua Landau joshua at landau.ws
Wed Aug 7 02:48:32 EDT 2013


On 6 August 2013 20:42, Luca Cerone <luca.cerone at gmail.com> wrote:
> Hi Chris, thanks
>
>> Do you ever instantiate any A() objects? You're attempting to call an
>>
>> unbound method without passing it a 'self'.
>
> I have tried a lot of variations, instantiating the object, creating lambda functions that use the unbound version of fun (A.fun.__func__) etc etc..
> I have played around it quite a bit before posting.
>
> As far as I have understood the problem is due to the fact that Pool pickle the function and copy it in the various pools..
> But since the methods cannot be pickled this fails..
>
> The same example I posted won't run in Python 3.2 neither (I am mostly interested in a solution for Python 2.7, sorry I forgot to mention that).
>
> Thanks in any case for the help, hopefully there will be some other advice in the ML :)


I think you might not understand what Chris said.

Currently this does *not* work with Python 2.7 as you suggested it would.

>>> op = map(A.fun,l)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method fun() must be called with A instance as
first argument (got int instance instead)

This, however, does:

>>> op = map(A(3).fun,l)
>>> op
[1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683]


Chris might have also been confused because once you fix that it works
in Python 3.

You will find that
http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-when-using-pythons-multiprocessing-pool-ma
explains the problem in more detail than I understand. I suggest
reading it and relaying further questions back to us. Or use Python 3
;).



More information about the Python-list mailing list