python 多处理池:我怎么知道池中的所有工作人员何时完成?

python multiprocessing pool: how can I know when all the workers in the pool have finished?

我 运行 在 python 中设置了一个多处理池,我在那里有大约 2000 个任务,被映射到池中的 24 个工作人员。 每个任务都会根据一些数据分析和网络服务创建一个文件。

我想 运行 一个新任务,当池中的所有任务都完成时。我怎么知道池中的所有进程何时完成?

您想使用 the join method,它会停止主进程线程继续前进,直到所有子进程结束:

Block the calling thread until the process whose join() method is called terminates or until the optional timeout occurs.

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    processes = []
    for i in range(10):
        p = Process(target=f, args=('bob',))
        processes.append(p)

    for p in processes:
        p.start()
        p.join()

     # only get here once all processes have finished.
     print('finished!')

编辑:

使用join with pools

    pool = Pool(processes=4)  # start 4 worker processes
    result = pool.apply_async(f, (10,))  # do some work
    pool.close()
    pool.join()  # block at this line until all processes are done
    print("completed")

可以使用ApplyResult对象的wait()方法(也就是pool.apply_asyncreturns)。

import multiprocessing

def create_file(i):
    open(f'{i}.txt', 'a').close()

if __name__ == '__main__':
    # The default for n_processes is the detected number of CPUs
    with multiprocessing.Pool() as pool:

        # Launch the first round of tasks, building a list of ApplyResult objects
        results = [pool.apply_async(create_file, (i,)) for i in range(50)]
    
        # Wait for every task to finish
        [result.wait() for result in results]

        # {start your next task... the pool is still available}

    # {when you reach here, the pool is closed}

即使您打算再次使用您的池并且不想关闭它,此方法也适用 - 例如,您可能希望在算法的下一次迭代中保留它。使用 with 语句或在使用完后手动调用 pool.close(),否则会发生不好的事情。