相关疑难解决方法(0)

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

当我运行类似的东西:

from multiprocessing import Pool

p = Pool(5)
def f(x):
     return x*x

p.map(f, [1,2,3])
Run Code Online (Sandbox Code Playgroud)

它工作正常.但是,将此作为类的函数:

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()
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

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
Run Code Online (Sandbox Code Playgroud)

我看过Alex Martelli的一篇文章处理同样的问题,但它不够明确.

python pickle multiprocessing

165
推荐指数
10
解决办法
9万
查看次数

标签 统计

multiprocessing ×1

pickle ×1

python ×1