What exactly is Python multiprocessing Module's .join() Method Doing?

前端 未结 6 888
梦如初夏
梦如初夏 2020-11-29 16:09

Learning about Python Multiprocessing (from a PMOTW article) and would love some clarification on what exactly the join() method is doing.

In an old tut

6条回答
  •  春和景丽
    2020-11-29 17:07

    The join() method, when used with threading or multiprocessing, is not related to str.join() - it's not actually concatenating anything together. Rather, it just means "wait for this [thread/process] to complete". The name join is used because the multiprocessing module's API is meant to look as similar to the threading module's API, and the threading module uses join for its Thread object. Using the term join to mean "wait for a thread to complete" is common across many programming languages, so Python just adopted it as well.

    Now, the reason you see the 20 second delay both with and without the call to join() is because by default, when the main process is ready to exit, it will implicitly call join() on all running multiprocessing.Process instances. This isn't as clearly stated in the multiprocessing docs as it should be, but it is mentioned in the Programming Guidelines section:

    Remember also that non-daemonic processes will be automatically be joined.

    You can override this behavior by setting the daemon flag on the Process to True prior to starting the process:

    p = Process(target=say_hello)
    p.daemon = True
    p.start()
    # Both parent and child will exit here, since the main process has completed.
    

    If you do that, the child process will be terminated as soon as the main process completes:

    daemon

    The process’s daemon flag, a Boolean value. This must be set before start() is called.

    The initial value is inherited from the creating process.

    When a process exits, it attempts to terminate all of its daemonic child processes.

提交回复
热议问题