Python multiprocessing PicklingError: Can't pickle

前端 未结 8 615
故里飘歌
故里飘歌 2020-11-22 03:48

I am sorry that I can\'t reproduce the error with a simpler example, and my code is too complicated to post. If I run the program in IPython shell instead of the regular Pyt

8条回答
  •  后悔当初
    2020-11-22 03:59

    I'd use pathos.multiprocesssing, instead of multiprocessing. pathos.multiprocessing is a fork of multiprocessing that uses dill. dill can serialize almost anything in python, so you are able to send a lot more around in parallel. The pathos fork also has the ability to work directly with multiple argument functions, as you need for class methods.

    >>> from pathos.multiprocessing import ProcessingPool as Pool
    >>> p = Pool(4)
    >>> class Test(object):
    ...   def plus(self, x, y): 
    ...     return x+y
    ... 
    >>> t = Test()
    >>> p.map(t.plus, x, y)
    [4, 6, 8, 10]
    >>> 
    >>> class Foo(object):
    ...   @staticmethod
    ...   def work(self, x):
    ...     return x+1
    ... 
    >>> f = Foo()
    >>> p.apipe(f.work, f, 100)
    
    >>> res = _
    >>> res.get()
    101
    

    Get pathos (and if you like, dill) here: https://github.com/uqfoundation

提交回复
热议问题