多处理Python池

问题描述:

在下面的情况下何时需要在Pool上调用.join().close()?阅读文档,它看起来像是等待流程完成。举例来说,如果我做这样的事情:多处理Python池

while True: 
    pool = Pool(processes=4) 
    results = [] 
    for x in range(1000): 
     result = pool.apply_async(f, (x,)) 
     results.append(result) 

    for result in results: 
     result.get(timeout=1) 
    print "finished" 

我还需要等待其他过程与join()close()完成?正如我所假设的那样,因为我正在迭代所有异步结果并等待(阻止)完成,到达print finished时,所有进程都已退出?

这是正确的吗?

此外,什么时候进程开始工作的功能?我注意到有4个进程与ps -elf并行运行。在这种情况下调用result.get()之后,这些过程是否只能在该功能上起作用?

接近()

被提交到池中防止任何更多的任务。所有任务完成后,工作进程将退出。

join()方法

等待所有进程正常终止

好链接只要您拨打pool.apply_async进程将开始在功能上工作,它开始与Proper way to use multiprocessor.Pool in a nested loop

将返回结果对象