我第一次在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
每次都挂掉。
答案 0 :(得分:3)
传递给t.map
needs to be picklable的函数。匿名函数(即lambda函数)are not picklable - 至少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]