Python 多处理:map vs map_async
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35908987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiprocessing: map vs map_async
提问by aman
What's the difference between using map
and map_async
? Are they not running the same function after distributing the items from the list to 4 processes?
usingmap
和 和有map_async
什么不一样?将列表中的项目分发给 4 个进程后,它们是否没有运行相同的功能?
So is it wrong to presume both are running asynchronous and parallel?
那么假设两者都在异步和并行运行是错误的吗?
def f(x):
return 2*x
p=Pool(4)
l=[1,2,3,4]
out1=p.map(f,l)
#vs
out2=p.map_async(f,l)
回答by quikst3r
There are four choices to mapping jobs to processes. You have to consider multi-args, concurrency, blocking, and ordering. map
and map_async
only differ with respect to blocking. map_async
is non-blocking where as map
is blocking
将作业映射到进程有四种选择。您必须考虑多参数、并发、阻塞和排序。map
并且map_async
仅在阻塞方面有所不同。map_async
是非阻塞的,而map
阻塞的则是
So let's say you had a function
假设你有一个函数
from multiprocessing import Pool
import time
def f(x):
print x*x
if __name__ == '__main__':
pool = Pool(processes=4)
pool.map(f, range(10))
r = pool.map_async(f, range(10))
# DO STUFF
print 'HERE'
print 'MORE'
r.wait()
print 'DONE'
Example output:
示例输出:
0
1
9
4
16
25
36
49
64
81
0
HERE
1
4
MORE
16
25
36
9
49
64
81
DONE
pool.map(f, range(10))
will wait for all 10 of those function calls to finish so we see all the prints in a row.
r = pool.map_async(f, range(10))
will execute them asynchronously and only block when r.wait()
is called so we see HERE
and MORE
in between but DONE
will always be at the end.
pool.map(f, range(10))
将等待所有 10 个这些函数调用完成,以便我们看到所有打印成一行。
r = pool.map_async(f, range(10))
将异步执行它们并且只在r.wait()
被调用时阻塞,所以我们看到HERE
和MORE
在两者之间,但DONE
总是在最后。