Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Joining Threads

Give Feedback

In this lesson, you’ll learn how to use the join() method to bring all your threads together before the main thread exits. If you download the sample code, you can get your own copy of 05-join.py:

Download

Sample Code (.zip)

12.9 KB

To learn more, you can also check out the documentation.

00:00 In the previous lesson, we saw how we can add daemon=True to create a thread that will be killed once our main program completes. In this lesson, we’re going to learn about how we can have our main thread wait for other threads to finish before it completes, and this waiting for other threads is what we call join.

00:22 I’m actually going to use the code from the previous lesson, I’m just going to copy all this code into a new file called 05-join,

00:32 and we’re going to make a slight modification here. We’re going to not make this a daemon thread because we don’t want that behavior in this example, and we’re going to add one more function here.

00:44 Before we do that, let’s go ahead and execute this program. I’ve saved the file, now I’m going to drop down into the terminal, 05-join, and we’ll go back.

00:55 We’ve seen this before. t has started, t is sleeping while we’re seeing main ended here,

01:03 and then we have myfunc ended. This is the non-daemonic behavior, if you will. Without the daemon=True we actually get main ended before myfunc ended, and we had t finish.

01:20 We didn’t kill t using the daemon=True flag. So, we’ve taken that away. We have t.start(). But what we want to do here is we want to actually have myfunc ended before main ended. In other words, we want t to finish before our main thread finishes.

01:42 We can do that by adding a function call to .join() here, on t. .join() is going to tell the main thread to wait for t to finish before continuing and finishing the program.

01:59 Let’s clear the screen and run that again.

02:03 We’ve added .join(), now let’s see what happens. We’ve got main started, right here in line 10. We created our thread, we started it, and we called .join().

02:14 Now, once t was started, it dropped up here—we saw myfunc started with realpython— this is coming from the t thread.

02:22 And then it slept—as we saw, we were hanging out there for 10 seconds. And then it printed myfunc ended. This print statement came from t and then we had t.join() called from the main thread, so that told the main thread, “Hey, I want you to wait until t finishes and then join the thread back with the main thread.” So then we see main ended.

02:46 This is basically the same behavior as if we were to just delete these three lines and call myfunc() explicitly with 'realpython'.

03:01 It is bringing more complexity into it, but we’re going to build upon that concept, and I just want you to see how we can use t.join() to have the main thread wait for other threads to finish before finishing itself.

gnraju24 on July 30, 2020

course is going great till now. thanks for explanation with code

kiran on Aug. 28, 2020

difference between join() & demon thread?

Bartosz Zaczyński RP Team on Aug. 31, 2020

Just a side note: it’s not “demon” but a “daemon” thread.

.join() is a method on the threading.Thread class. You can call it from another thread to wait until the other one finishes its execution. It doesn’t matter whether you’re calling .join() on a regular or a daemon thread.

Regular threads running in the background will prevent Python from exiting even after the main thread has finished. On the other hand, daemon threads don’t have such power. They’ll stop abruptly when the main thread is done. For this reason, you should never use resources that require cleanup, such as files, in daemon threads.

Become a Member to join the conversation.