Python Thread - join() method

Join method in Python makes the calling thread to wait for another thread. The wait could be till another thread terminates or based on a timeout value.

Thread Updated 21 Sep 2016 2 min read

Method Name

join()

Method Overview

When join() method is invoked, the calling thread is blocked till the thread object on which it was called is terminated.

For example, when the join() is invoked from a main thread, the main thread waits till the child thread on which join is invoked exits.
The significance of join() method is, if join() is not invoked, the main thread may exit before the child thread, which will result undetermined behaviour of programs and will affect program invariants and integrity of the data on which the program operates.

The join() method can also be specified of a timeout value.

For example, a thread that makes network connections to servers is expected to complete the connection establishment within stipulated number of seconds. When the timeout value elapses the calling thread will come from the blocking state and could try connecting to a set of backup servers from a config file.

In such circumstances calling thread may send a signal through event object and ask the calling thread to stop. The join() method can be called several times on a thread object.

Exceptions

Calling join() on the same thread will result in a deadlock. Hence, a RuntimeError is raised when join() is invoked on the same thread. Calling join() on a thread which has not yet been started also causes a RuntimeError.

Example

python
from threading import Thread

from threading import Event

import time

class ConnectionThread(Thread):

    myStopEvent = 0

    def __init__(self,args):

        Thread.__init__(self)

        self.myStopEvent = args

    # The run method is overridden to define the thread body

    def run(self):

        for i in range(1,10):

            if(self.myStopEvent.wait(0)):

                print ("ChildThread:Asked to stop")

                break;

            print("ChildThread:Sleep count %d"%(i))

            time.sleep(3)

        print ("ChildThread:Exiting")

aStopEvent = Event()

ConnectionThread = ConnectionThread(aStopEvent)

ConnectionThread.start()

print("Main thread: Starting to wait for 5 seconds")

ConnectionThread.join(5)

print("Main thread: I cant't wait for more than 5 seconds for the child thread;Will ask child thread to stop")

aStopEvent.set()   #ask(signal) the child thread to stop

ConnectionThread.join() # wait for the child thread to stop

print("Main thread: Now I do something else to compensate the child thread task and exit")

print("Main thread: Exiting")

Output

Output
ChildThread:Sleep count 1

Main thread: Starting to wait for 5 seconds

ChildThread:Sleep count 2

Main thread: I cant't wait for more than 5 seconds for the child thread;Will ask child thread to stop

ChildThread:Asked to stop

ChildThread:Exiting

Main thread: Now I do something else to compensate the child thread task and exit

Main thread: Exiting