使用map_async()时,我似乎无法使回调工作。当我使用稍微修改过的代码循环遍历我的数组时,它通过apply_async()来添加任务。从文档中看来,我应该能够使用map_async()的回调,但也许是某种新的错误......
from multiprocessing import Pool,TimeoutError
from time import sleep
servers=["s1","s2","s3","s4","s5","s6"]
def f(x):
print("start f(" + x + ")")
sleep(5)
print("end f(" + x + ")")
return "did " + x
def mycallback(x):
print("My callback " + str(x))
def myerrorcallback(r):
print("My errorcallback " + str(r))
if __name__ == '__main__':
pool = Pool(processes=4)
results = pool.map_async(f, servers, chunksize=1, callback=mycallback, error_callback=myerrorcallback)
print(results.get(timeout=11))
跑步时我得到:
D:\python> f.py
start f(s1)
start f(s2)
start f(s3)
start f(s4)
end f(s1)
start f(s5)
end f(s2)
start f(s6)
end f(s4)
end f(s3)
end f(s5)
end f(s6)
['did s1', 'did s2', 'did s3', 'did s4', 'did s5', 'did s6']
当我使用apply_async()修改代码时,我从回调中获取打印输出。修改后的代码只是将最后一部分更改为:
if __name__ == '__main__':
pool = Pool(processes=4)
for server in servers:
pool.apply_async(f, (server,), callback=mycallback, error_callback=myerrorcallback)
pool.close()
pool.join()
结果:
D:\python\>fb.py
start f(s1)
start f(s2)
start f(s3)
start f(s4)
end f(s1)
start f(s5)
My callback did s1
end f(s2)
My callback did s2
start f(s6)
end f(s3)
My callback did s3
end f(s4)
My callback did s4
end f(s5)
My callback did s5
end f(s6)
My callback did s6
答案 0 :(得分:2)
好的我抓住机会并记录了一个错误。事实证明它实际上是3.3中的一个错误,补丁正在进行中。
答案 1 :(得分:1)
在python 3.4.3
中,使用累计结果调用回调,但忽略回调的返回值。有什么意义?
from multiprocessing import Pool
data = [1, 2, 3]
def calc(x):
return 2*x
def increment(results):
print('callback called')
return [x+100 for x in results]
with Pool(None) as pool: #None => calls cpu.count()
results = pool.map_async(calc, data, callback=increment)
print(results.get())
--output:--
callback called
[2, 4, 6]