为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

12345678_0001 2019-10-05 11:24:27
当我运行类似:from multiprocessing import Poolp = Pool(5)def f(x):     return x*xp.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的一篇文章,涉及类似的问题,但还不够明确。
查看完整描述

3 回答

?
达令说

TA贡献1821条经验 获得超6个赞

我也对pool.map可以接受哪种功能的限制感到恼火。我写了以下内容来规避这一点。即使递归使用parmap,它似乎也可以工作。


from multiprocessing import Process, Pipe

from itertools import izip


def spawn(f):

    def fun(pipe,x):

        pipe.send(f(x))

        pipe.close()

    return fun


def parmap(f,X):

    pipe=[Pipe() for x in X]

    proc=[Process(target=spawn(f),args=(c,x)) for x,(p,c) in izip(X,pipe)]

    [p.start() for p in proc]

    [p.join() for p in proc]

    return [p.recv() for (p,c) in pipe]


if __name__ == '__main__':

    print parmap(lambda x:x**x,range(1,5))


查看完整回答
反对 回复 2019-10-05
  • 3 回答
  • 0 关注
  • 734 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信