How to get around the pickling error of python multiprocessing without being in the top-level?

前端 未结 3 839
忘了有多久
忘了有多久 2021-02-14 12:31

I\'ve researched this question multiple times, but haven\'t found a workaround that either works in my case, or one that I understand, so please bear with me.

Basically

3条回答
  •  佛祖请我去吃肉
    2021-02-14 12:45

    You could use pathos.multiprocessing, which is a fork of multiprocessing that uses the dill serializer instead of pickle. dill can serialize pretty much anything in python. Then, no need to edit your code.

    >>> from pathos.multiprocessing import ProcessingPool as Pool
    >>> 
    >>> def calculate(x):
    ...   def domath(y):
    ...     return x*y
    ...   return Pool().map(domath, range(3))
    ... 
    >>> calculate(2)
    [0, 2, 4]
    

    You can even go nuts with it… as most things are pickled. No need for the odd non-pythonic solutions you have to cook up with pure multiprocessing.

    >>> class Foo(object):
    ...   def __init__(self, x):
    ...     self.x = x
    ...   def doit(self, y):
    ...     return ProcessingPool().map(self.squared, calculate(y+self.x))
    ...   def squared(self, z):
    ...     return z*z
    ... 
    >>> def thing(obj, y):
    ...   return getattr(obj, 'doit')(y)
    ... 
    >>> ProcessingPool().map(thing, ProcessingPool().map(Foo, range(3)), range(3))
    [[0, 0, 0], [0, 4, 16], [0, 16, 64]]
    

    Get pathos here: https://github.com/uqfoundation

提交回复
热议问题