Python .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 threadingdef 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...TrueFinishedStarting...FalseFinished
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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours