Process.join

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:

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:

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

 


Copyright 2024 © pythontic.com