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

use*_*768 19 python pool multiprocessing

我花了整整一夜调试我的代码,最后我发现了这个棘手的问题.请看下面的代码.

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()
Run Code Online (Sandbox Code Playgroud)

我以为我会得到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]]?提前感谢您的任何意见.

fal*_*tru 35

[[0], [0, 1]]如果使用map_async,则使用result()调用一次回调.

>>> 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]]
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

  • @GamesBrainiac,[(在Windows中)](http://docs.python.org/2/library/multiprocessing.html#windows). (7认同)
  • 这段代码应该不起作用,你需要把它放在一个 `if __name__ == '__main__'` 语句下 (2认同)
  • 哦!那不好意思了。你们幸运的 mac/linux 家伙 :P (2认同)