Python - Joining the Threads



The join() method in thread class blocks the calling thread until the thread whose join() method is called terminates. The termination may be either normal, because of an unhandled exception − or until the optional timeout occurs. It can be called many times. The join() raises a RuntimeError if an attempt is made to join the current thread. Attempt to join() a thread before it has been started also raises the same exception.

Syntax

thread.join(timeout)

Parameters

  • timeout − it should be a floating point number specifying a timeout for which the thread is to be blocked.

The join() method always returns None. you must call is_alive() after join() to decide whether a timeout happened − if the thread is still alive, the join() call timed out. When the timeout argument is not present or None, the operation will block until the thread terminates.

A thread can be joined many times.

Example

thread1.start()
thread2.start()
thread1.join()
thread2.join()

is_alive() method

This method returns whether the thread is alive. It returns True just before calling run() method and until just after the run() method terminates.

Advertisements