Process.join

Process.join() method, makes the calling process wait for the process instance on which it was called to complete. In the Python example the main process creates three processes and wait for all of them to complete using the join() method.

Process Updated 3 Feb 2019 1 min read

Method Name

join

Method Signature

join()

Return Value

NoneType

Parameters

Not Applicable

Method Overview

  • Calling join() on a Process instance makes the calling process to wait for the corresponding process.
  • Once the process is complete the called process proceeds.
  • In the examples provided on multiprocessing it is the main process that calls the join() method on the Process instances corresponding the child processes.
  • Calling join() on the Process instance for the same process results in deadlock.
  • join() can be called only on an already started process.
  • join() is similar to a person waiting for another person to complete something and proceeding after it.

Example

python
import multiprocessing as multiproc

def processCategory1(mq):

                mq.put("Category1 message")

def processCategory2(mq):

                mq.put("Category2 message")

def processCategory3(mq):

                mq.put("Category3 message")

if __name__=="__main__":

                print("Parent process started")

                multiproc.set_start_method = "forkserver"

    mq  = multiproc.Queue()

    # Create child processes

                childProcess1 = multiproc.Process(target = processCategory1, args=(mq,), name="Cat1")

                childProcess2 = multiproc.Process(target = processCategory2, args=(mq,), name="Cat2")

                childProcess3 = multiproc.Process(target = processCategory3, args=(mq,), name="Cat3")

    # Start all the child processes

                childProcess1.start()

                childProcess2.start()

                childProcess3.start()

    print("Child processes started....main process waiting for children to complete")

    # Wait for all the child processes to complete

                childProcess1.join()

                childProcess2.join()

                childProcess3.join()

    print("Child processes completed execution")

    while mq.empty() is False:

                    print(mq.get())

    print("Parent process ending")

Output

Output
Parent process started

Child processes started....main process waiting for children to complete

Child processes completed execution

Category1 message

Category2 message

Category3 message

Parent process ending

##