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)
| 归档时间: |
|
| 查看次数: |
29331 次 |
| 最近记录: |