python3 中的多处理无法正常工作

MultiProcessing in python3 not working

我是第一次在 python3 中使用多重处理。这是我要实现的:

import concurrent
t = concurrent.futures.ProcessPoolExecutor(4)
g = t.map(lambda x:10*x, range(10))

它正在抛出错误:

File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/queues.py", line 242, in _feed
    obj = ForkingPickler.dumps(obj)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/reduction.py", line 50, in dumps
    cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function <lambda> at 0x102f03730>: attribute lookup <lambda> on __main__ failed

每次都出去玩。

传递给t.mapneeds to be picklable. Anonymous functions (i.e. lambda functions) are not picklable -- at least not by default的函数。要修复,请在全局命名空间中定义函数:

import concurrent.futures as CF
def func(x):
    return 10*x

if __name__ == '__main__':
    with CF.ProcessPoolExecutor(4) as t:
        g = t.map(func, range(10))
        print(list(g))
        # [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]