【问题标题】:How to wait for all Processes to end如何等待所有进程结束
【发布时间】:2015-10-25 23:20:21
【问题描述】:

我已经并行运行了两个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其他多处理/线程库时的等价物是什么?

【问题讨论】:

  • 这是join(),而不是wait()。这适用于multiprocessing.Processthreading.Thread。此外,粘贴的代码将失败,因为您试图将参数传递给您的两个函数,但没有一个函数接受参数。看起来你想要def foobar(x):def barfoo(x):

标签: python multithreading subprocess python-multiprocessing


【解决方案1】:

使用Process.join,它本质上是“等待”的线程术语:

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

【讨论】:

    【解决方案2】:

    您可以使用join方法等待它结束。

    thread_fb.join()
    thread_bf.join()
    

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

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 2016-06-15
      • 1970-01-01
      相关资源
      最近更新 更多