Here is a code to demo my question:
from multiprocessing import Process
def worker():
print("Worker running")
if __name__ == "__main__":
p = Process(target=worker)
p.start()
input("1...")
input("2...")
p.join()
Note, ran on Python 3.13, Windows x64.
And the output I got is (after inputting Enter
twice):
1...
2...
Worker running
Process finished with exit code 0
From the output, we can see the process actually initialized and started to run after the 2nd input. While I thought start()
should block and guarantee the child process is fully initialized.
Is this a normal behavior of Python multiprocessing?
Because if Threading is used here instead, this issue seldom occur. I always get the thread run before the line input("1...")
.
May I ask, if Process.start()
doesn't guarantee the process is fully-started, how should we code to ensure the child process is actually running before proceeding in the parent?
This is normal behaviour, and it's usually exactly what you want when you choose multiprocessing over, say, threading, i.e., the processes continue in parallel and do not block each other.
As mentioned in the comments, here's an example how you can make sure the worker is running before proceeding:
import time
from multiprocessing import Process, Event
def worker(start_event):
print("Worker started")
start_event.set()
print("Worker is doing some work")
time.sleep(2)
if __name__ == "__main__":
start_event = Event()
p = Process(target=worker, args=(start_event,))
p.start()
start_event.wait()
print("Worker has started. Continuing main process.")
print("Waiting for worker to finish")
p.join()
A common pattern, however, is to communicate with the worker via a work queue and a stop event (or some other means) to tell it to shut down.