多重处理:如何在类中定义的函数上使用Pool.map?

当我运行类似:


from multiprocessing import Pool


p = Pool(5)

def f(x):

     return x*x


p.map(f, [1,2,3])

它工作正常。但是,将其作为类的函数:


class calculate(object):

    def run(self):

        def f(x):

            return x*x


        p = Pool()

        return p.map(f, [1,2,3])


cl = calculate()

print cl.run()

给我以下错误:


Exception in thread Thread-1:

Traceback (most recent call last):

  File "/sw/lib/python2.6/threading.py", line 532, in __bootstrap_inner

    self.run()

  File "/sw/lib/python2.6/threading.py", line 484, in run

    self.__target(*self.__args, **self.__kwargs)

  File "/sw/lib/python2.6/multiprocessing/pool.py", line 225, in _handle_tasks

    put(task)

PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

我看过Alex Martelli的一篇文章,涉及类似的问题,但还不够明确。


12345678_0001
浏览 735回答 3
3回答

达令说

我也对pool.map可以接受哪种功能的限制感到恼火。我写了以下内容来规避这一点。即使递归使用parmap,它似乎也可以工作。from multiprocessing import Process, Pipefrom itertools import izipdef spawn(f):&nbsp; &nbsp; def fun(pipe,x):&nbsp; &nbsp; &nbsp; &nbsp; pipe.send(f(x))&nbsp; &nbsp; &nbsp; &nbsp; pipe.close()&nbsp; &nbsp; return fundef parmap(f,X):&nbsp; &nbsp; pipe=[Pipe() for x in X]&nbsp; &nbsp; proc=[Process(target=spawn(f),args=(c,x)) for x,(p,c) in izip(X,pipe)]&nbsp; &nbsp; [p.start() for p in proc]&nbsp; &nbsp; [p.join() for p in proc]&nbsp; &nbsp; return [p.recv() for (p,c) in pipe]if __name__ == '__main__':&nbsp; &nbsp; print parmap(lambda x:x**x,range(1,5))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python