【发布时间】:2015-10-24 12:26:22
【问题描述】:
从map 中断按预期工作:
def worker(x):
print("worker call x=%s" % x)
return x
for x in map(worker, range(5)):
print(x)
if x == 2:
break
worker call x=0
0
worker call x=1
1
worker call x=2
2
但如果我对 multiprocessing 做同样的事情,我会得到:
from multiprocessing import Pool
pool = Pool(2)
for x in pool.map(worker, range(5)):
print(x)
if x == 2:
break
pool.close()
pool.join()
0
1
2
worker call x=0
worker call x=1
worker call x=2
worker call x=3
worker call x=4
为什么多处理的地图表现不同?如何避免不必要的函数调用?
【问题讨论】:
标签: python python-3.x python-multiprocessing