Skip to main content
 首页 » 编程设计

python之Multiprocessing Pool 非阻塞检测完成的任务

2024年10月01日12yjmyzz

有没有办法知道进程池已经完成运行任务?

_process_pool = mp.Pool(processes=num_workers) 

我正在分批将任务添加到池中:

for batch in gen_batches(): 
   _process_pool.map_async(fn, batch) 

有没有办法知道所有任务何时完成? callback 在这里似乎不起作用。我想通过调用 _process_pool.join()

来避免阻塞父进程

请您参考如下方法:

Question: Is there a way to know when all tasks have been done?

将 pool.map_async(... 中的所有 AsyncResult 附加到 list 中,例如:

multiple_results = [] 
for batch in gen_batches(): 
   multiple_results.append( _process_pool.map_async(fn, batch) ) 
 
 
if all([ar.ready() for ar in multiple_results]): 
    print('Pool done') 
else: 
    print('Pool any alive') 

Python » 3.6.1 Documentation: multiprocessing.pool.AsyncResult

使用 Python:3.4.2 测试