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

Luca Cerone luca.cerone at gmail.com
Tue Aug 6 15:37:45 EDT 2013


On Tuesday, 6 August 2013 18:12:26 UTC+1, Luca Cerone  wrote:
> Hi guys,
> 
> I would like to apply the Pool.map method to a member of a class.
> 
> 
> 
> Here is a small example that shows what I would like to do:
> 
> 
> 
> from multiprocessing import Pool
> 
> 
> 
> class A(object):
> 
>    def __init__(self,x):
> 
>        self.value = x
> 
>    def fun(self,x):
> 
>        return self.value**x
> 
> 
> 
> 
> 
> l = range(10)
> 
> 
> 
> p = Pool(4)
> 
> 
> 
> op = p.map(A.fun,l)
> 
> 
> 
> #using this with the normal map doesn't cause any problem
> 
> 
> 
> This fails because it says that the methods can't be pickled.
> 
> (I assume it has something to do with the note in the documentation: "functionality within this package requires that the __main__ module be importable by the children.", which is obscure to me).
> 
> 
> 
> I would like to understand two things: why my code fails and when I can expect it to fail? what is a possible workaround?
> 
> 
> 
> Thanks a lot in advance to everybody for the help!
> 
> 
> 
> Cheers,
> 
> Luca



On Tuesday, 6 August 2013 18:12:26 UTC+1, Luca Cerone  wrote:
> Hi guys,
> 
> I would like to apply the Pool.map method to a member of a class.
> 
> 
> 
> Here is a small example that shows what I would like to do:
> 
> 
> 
> from multiprocessing import Pool
> 
> 
> 
> class A(object):
> 
>    def __init__(self,x):
> 
>        self.value = x
> 
>    def fun(self,x):
> 
>        return self.value**x
> 
> 
> 
> 
> 
> l = range(10)
> 
> 
> 
> p = Pool(4)
> 
> 
> 
> op = p.map(A.fun,l)
> 
> 
> 
> #using this with the normal map doesn't cause any problem
> 
> 
> 
> This fails because it says that the methods can't be pickled.
> 
> (I assume it has something to do with the note in the documentation: "functionality within this package requires that the __main__ module be importable by the children.", which is obscure to me).
> 
> 
> 
> I would like to understand two things: why my code fails and when I can expect it to fail? what is a possible workaround?
> 
> 
> 
> Thanks a lot in advance to everybody for the help!
> 
> 
> 
> Cheers,
> 
> Luca



More information about the Python-list mailing list