如何等待所有进程结束

How to wait for all Processes to end

我有 运行 两个 Process 并行实例:

from multiprocessing import Process

def foobar():
  return x*x

def barfoo():
  return x+x

thread_fb = Process(target = foobar, args=(3,)))
thread_bf = Process(target = barfoo, args=(3,)))
thread_fb.start(); thread_bf.start()
thread_fb.wait(); thread_bf.wait()

它抛出了这个错误:

AttributeError: 'Process' object has no attribute 'wait'

如何等待全部multiprocessing.Process结束?

使用 threading 其他多 processing/threading 库时的等价物是什么?

可以使用join方法等待结束

thread_fb.join()
thread_bf.join()

此外,线程和进程不同,因此您可能需要考虑更改名称。

使用Process.join,本质上是"wait"的线程行话:

thread_fb.start()
thread_bf.start()
thread_fb.join()
thread_bf.join()