pstatix
pstatix

Reputation: 3848

Pausing main process until spawned process begins execution?

How can I keep the main process from continuing until a spawned process has begun its execution?

Suppose I had the following simple example:

import multiprocessing as mp

def foo():
    print ("In 'foo'")
    while True:
        pass

def bar():
    print ("In 'bar'")
    count = 0
    while count < 5001:
        count += 1

def main():
    print ("In 'main'")
    p = mp.Process(target = foo, args = ())
    p.start()
    # stop here until 'foo()' prints
    bar()

if __name__ == '__main__':
    main()

As I understand it, when a Process.start() occurs, the process has to "reimport" everything from __main__, and because of this, in my program there is a delay from when foo() would begin, yet __main__ continues while the new process is starting up.

The only working method I have is using a multiprocessing.Pipe():

import multiprocessing as mp

def foo(s):
    print ("In 'foo'")
    s.close()
    while True:
        pass

def bar():
    print ("In 'bar'")
    count = 0
    while count < 5001:
        count += 1

def main():
    print ("In 'main'")
    r, s = mp.Pipe()
    p = mp.Process(target = foo, args = (s,))
    p.start()
    while not s.closed:
        pass
    bar()

if __name__ == '__main__':
    main()

But this seems clunky since I dont even use the Pipe() for what it is meant for. Another method I thought would work was to use a multiprocessing.Lock(), but because of the "reimport" delay, the target method does acquire the lock before bar() is executed on __main__.

Is there a better way to handle this?

Upvotes: 3

Views: 85

Answers (1)

Yassine Faris
Yassine Faris

Reputation: 991

You can use Event. You can make you main process wait for the event to be set before continuing. And your child process will set the event when it start in your target function.

import multiprocessing as mp
import time


def foo(process_started):
    print ("In 'foo'")
    time.sleep(5)  # Sleep to show that the main process is waiting for the event
    process_started.set()
    while True:
        pass

def bar():
    print ("In 'bar'")
    count = 0
    while count < 5001:
        count += 1

def main():
    print ("In 'main'")
    process_started = mp.Event()
    p = mp.Process(target = foo, args = (process_started,))
    p.start()
    process_started.wait()  # Wait for the Event to be set
    bar()

if __name__ == '__main__':
    main()

Upvotes: 2

Related Questions