alvas
alvas

Reputation: 122240

How to wait for all Processes to end

I've ran some two Process instances in parallel:

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()

It threw this error:

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

How to wait for all multiprocessing.Process to end?

What is the the equivalent when using threading other multi processing/threading library?

Upvotes: 1

Views: 2041

Answers (2)

kirbyfan64sos
kirbyfan64sos

Reputation: 10727

Use Process.join, which is essentially threading jargon for "wait":

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

Upvotes: 3

Eric Renouf
Eric Renouf

Reputation: 14520

You can use the join method to wait for it to end.

thread_fb.join()
thread_bf.join()

Also, threads and processes aren't the same, so you might want to consider name changes.

Upvotes: 2

Related Questions