Python 3.x python3中的多处理不起作用

Python 3.x python3中的多处理不起作用,python-3.x,multiprocessing,concurrent.futures,Python 3.x,Multiprocessing,Concurrent.futures,我第一次在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/

我第一次在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
文件“/usr/local/ceral/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/queues.py”,第242行,在_提要中
obj=分叉pickler.dumps(obj)
文件“/usr/local/ceral/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/reduce.py”,第50行,转储
cls(buf,协议).dump(obj)
_pickle.PicklingError:无法pickle:在_main上查找属性失败

并且每次都挂起。

传递给
t.map的函数。匿名函数(即lambda函数)——至少。要修复此问题,请在全局命名空间中定义函数:

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]