多处理Python P

2024-05-21 07:35:00 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的情况下,何时需要对Pool调用.join()和{}?在阅读文档时,它看起来像是在等待进程完成。例如,如果我这样做:

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()之后才开始对函数起作用?在


Tags: 函数in文档forget进程情况result
1条回答
网友
1楼 · 发布于 2024-05-21 07:35:00

close()

Prevents any more tasks from being submitted to the pool. Once all the tasks have been completed the worker processes will exit.

join()

Wait for all processes to properly terminate

Proper way to use multiprocessor.Pool in a nested loop开头的链接不错

一旦您调用pool.apply_async,进程将开始处理该函数,它将返回一个result对象

相关问题 更多 >