Python multiprocessing.pools.map\u异步回调不工作?

Python multiprocessing.pools.map\u异步回调不工作?,python,Python,使用map\u async()时,我似乎无法使回调正常工作。当我使用稍加修改的代码来循环我的数组时,它会起作用,而是通过apply\u async()添加任务。从文档中可以看出,我应该能够使用带有map\u async()的回调,但可能是某种新的错误 from multiprocessing import Pool,TimeoutError from time import sleep servers=["s1","s2","s3","s4","s5","s6"] def f(x):

使用
map\u async()
时,我似乎无法使回调正常工作。当我使用稍加修改的代码来循环我的数组时,它会起作用,而是通过
apply\u async()
添加任务。从文档中可以看出,我应该能够使用带有
map\u 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

好吧,我冒了个险,记录了一个bug。事实证明它实际上是3.3中的一个bug,并且正在进行补丁


我无法再现您的输出,在我的机器上,它会额外输出一行“我的回调['did s1'、'did s2'、'did s3'、'did s4'、'did s5'、'did s6']”。你能发布你的“修改”代码吗?哪一版本的python?我在windows上使用了3.3。我还编辑了我的问题,添加了有效的代码。它在python3.2上打印回调的输出,但在Ubuntu上不打印python3.3