Python 回调函数如何在多处理 map_async 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19699165/
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 14:23:56  来源:igfitidea点击:

How does the callback function work in multiprocessing map_async?

pythonmultiprocessingpool

提问by user2727768

It cost me a whole night to debug my code, and I finally found this tricky problem. Please take a look at the code below.

调试我的代码花了我整整一个晚上,我终于找到了这个棘手的问题。请看下面的代码。

from multiprocessing import Pool

def myfunc(x):
    return [i for i in range(x)]

pool=Pool()

A=[]
r = pool.map_async(myfunc, (1,2), callback=A.extend)
r.wait()

I thought I would get A=[0,0,1], but the output is A=[[0],[0,1]]. This does not make sense to me because if I have A=[], A.extend([0])and A.extend([0,1])will give me A=[0,0,1]. Probably the callback works in a different way. So my question is how to get A=[0,0,1]instead of [[0],[0,1]]?

我以为我会得到A=[0,0,1],但输出是A=[[0],[0,1]]. 这对我来说没有意义,因为如果我有A=[]A.extend([0])并且A.extend([0,1])会给我A=[0,0,1]。回调可能以不同的方式工作。所以我的问题是如何获取A=[0,0,1]而不是[[0],[0,1]]?

采纳答案by falsetru

Callback is called once with the result ([[0], [0, 1]]) if you use map_async.

[[0], [0, 1]]如果您使用 map_async,则使用结果 ( )调用一次回调。

>>> from multiprocessing import Pool
>>> def myfunc(x):
...     return [i for i in range(x)]
... 
>>> A = []
>>> def mycallback(x):
...     print('mycallback is called with {}'.format(x))
...     A.extend(x)
... 
>>> pool=Pool()
>>> r = pool.map_async(myfunc, (1,2), callback=mycallback)
>>> r.wait()
mycallback is called with [[0], [0, 1]]
>>> print(A)
[[0], [0, 1]]

Use apply_asyncif you want callback to be called for each time.

apply_async如果您希望每次都调用回调,请使用。

pool=Pool()
results = []
for x in (1,2):
    r = pool.apply_async(myfunc, (x,), callback=mycallback)
    results.append(r)
for r in results:
    r.wait()