我正在尝试使用池并行地部署一些子进程调用。如果我为池构建一个完整的可迭代项并使用imap
,map
,imap_unordered
等,一切正常,但我无法使apply_async
起作用。< / p>
例如,这可以正常工作:
from subprocess import check_call
from multiprocessing import Pool
def dispatch_call(file_name):
return check_call(...)
if __name__ == '__main__':
files = (constructed file list)
pool = Pool()
pool.imap(dispatch_call, files)
pool.close()
pool.join()
然而,这不是:
from subprocess import check_call
from multiprocessing import Pool
def dispatch_call(file_name):
return check_call(...)
if __name__ == '__main__':
files = (constructed file list)
pool = Pool()
for f in files:
pool.apply_async(dispatch_call, f)
pool.close()
pool.join()
我检查了multiprocessing
的文档,并且这些Windows特定问题似乎都不相关。我只是因为试图让它在Windows上运行而运气不好吗?
答案 0 :(得分:2)
您应该将回调参数作为序列(list / tuple)传递。
str
对象也是一个序列,但传递一个字符串会导致多个参数传递给回调(字符串的每个字符都被视为参数):
pool.apply_async(dispatch_call, [f])
OR
pool.apply_async(dispatch_call, (f,))