.join()

Published Jun 3, 2022
Contribute to Docs

The .join() method delays a program’s flow of execution until the target thread has been completely read.

Syntax

thread_object.join(timeout)

The .join() method always returns None. There is also a timeout parameter that is set to None by default. If set otherwise, it must be represented in seconds with a floating-point value.

Example

The following example features two threads, thread_A and thread_B. Each thread makes a call to .start(), immediately followed by a call to .join().

import threading
def is_divisible(dividend, divisor):
print("Starting...")
if(dividend % divisor == 0):
print(True)
else:
print(False)
print("Finished")
thread_A = threading.Thread(target=is_divisible, args=(28, 14))
thread_B = threading.Thread(target=is_divisible, args=(34, 7))
thread_A.start()
thread_A.join()
thread_B.start()
thread_B.join()

The output will look like this:

Starting...
True
Finished
Starting...
False
Finished

The second thread, thread_B, cannot start before thread_A is finished due to .join().

Codebyte Example

In the example below, the .join() method allows the last print() statement to appear at the expected time in the program’s flow of execution:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy