Python Windows上的multiprocessing.Pool.apply\u async
我正在尝试使用一个池来并行分配一些子进程调用。如果我为池构造一个完整的iterable,并使用Python Windows上的multiprocessing.Pool.apply\u async,python,windows,multiprocessing,pool,Python,Windows,Multiprocessing,Pool,我正在尝试使用一个池来并行分配一些子进程调用。如果我为池构造一个完整的iterable,并使用imap、map、imap\u unordered等,那么一切都可以正常工作。但我无法让应用异步工作 例如,这是正确的: from subprocess import check_call from multiprocessing import Pool def dispatch_call(file_name): return check_call(...) if __name__ == '
imap
、map
、imap\u unordered
等,那么一切都可以正常工作。但我无法让应用异步
工作
例如,这是正确的:
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()
我检查了文档中的
多处理
,这里似乎没有与Windows相关的问题。我只是运气不好,无法在Windows上运行吗?您应该将回调参数作为序列(列表/元组)传递
str
对象也是一个序列,但传递字符串会导致向回调传递多个参数(字符串的每个字符都被视为参数):
或
pool.apply_async(dispatch_call, [f])
pool.apply_async(dispatch_call, (f,))