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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:08:16  来源:igfitidea点击:

multiprocessing: map vs map_async

pythonpython-multiprocessing

提问by aman

What's the difference between using mapand 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. mapand map_asynconly differ with respect to blocking. map_asyncis non-blocking where as mapis 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 HEREand MOREin between but DONEwill always be at the end.

pool.map(f, range(10))将等待所有 10 个这些函数调用完成,以便我们看到所有打印成一行。 r = pool.map_async(f, range(10))将异步执行它们并且只在r.wait()被调用时阻塞,所以我们看到HEREMORE在两者之间,但DONE总是在最后。