[Python] Why doesn't multiprocessing.Process.start() in Python guarantee that the process has...

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página