can't use multiprocessing with class factory?

Robert Kern robert.kern at gmail.com
Fri Jan 28 14:23:35 EST 2011


On 1/28/11 1:02 PM, Alan wrote:
> Can the below example be fixed to work?
> Thanks,
> Alan Isaac
>
> import multiprocessing as mp
>
> class Test(object):
>      pass
>
> def class_factory(x):
>      class ConcreteTest(Test):
>          _x = x
>      return ConcreteTest
>
> def f(cls):
>      print cls._x
>
> if __name__ == '__main__':
>      pool = mp.Pool(2)
>      pool.map(f, [class_factory(i) for i in range(4)])

Send the (pickleable) factory and the arguments used to construct the instance, 
not the unpickleable instance itself.

def g(factory, i):
     cls = factory(i)
     print cls._x

if __name__ == '__main__':
       pool = mp.Pool(2)
       pool.map(g, zip([class_factory] * 4, range(4)))

By the way, when asking for help like this, show us what your code did and 
describe what results you want. It can often be hard to figure out exactly what 
you mean by "work".

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list