can't use multiprocessing with class factory?

Daniel Urban urban.dani at gmail.com
Fri Jan 28 14:25:08 EST 2011


On Fri, Jan 28, 2011 at 20:02, Alan <alan.isaac at gmail.com> 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)])

Only classes defined on the top level of a module are picklable (see
http://docs.python.org/dev/py3k/library/pickle#what-can-be-pickled-and-unpickled
). The collections.namedtuple class factory function works around this
limitation by setting the __module__  attribute of the created class,
but I'm not sure if this solution can be used in this case.


Daniel



More information about the Python-list mailing list