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.

Multiple Threads

Give Feedback

In this lesson, you’ll create a multi-threaded program and see how sometimes threads don’t finish completing in the order you might expect. If you download the sample code, you can get your own copy of 06-many_threads.py:

Download

Sample Code (.zip)

12.9 KB

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

00:00 In the previous video, we saw how we can use .join() to tell the main thread to wait for other threads to finish before ending the program. I’m going to go ahead and open up the code from the previous lesson and just copy all of that into a new file called 06-many_threads.py because I want to show how this can get very repetitive when you have more than one thread and more than one function.

00:32 I’m going to paste myfunc() a couple more times. Let’s make three functions and we’re going to have to give these different names, so I’m going to just call them myfunc1()—and for learning purposes so we can really understand what’s going on, let’s change our print statements as well. This one will be myfunc2().

00:51 I want you to kind of rename these functions. And we’ll say myfunc3().

01:03 And then now that we have three different functions, we want to create a thread for each one. We’re going to go ahead and copy these lines here and paste them in two more times—one for each function.

01:21 Now t will be t1 and it’s targeting myfunc1(). And then we’ll do t1.start(), t1.join(), and then t2—I think you understand what we’re doing here.

01:35 Let’s change the argument this time. Let’s just do something else for myfunc2(). And then we’ll say t3 is myfunc3(), and how about 'bar' for this one.

01:50 We’ll start t3, and we’ll join t3. So now that we have three threads targeting three different functions,

02:00 we are going to start them. And the way .join() works is actually we would want to do these joins at the end, after starting all the other threads.

02:10 We’re going to do t1.join(), t2.join(), and t3.join() after we’ve already started.

02:18 Now let’s go ahead and execute our program, so I’m going to come into the terminal and do 06-many_threads. And then we’ll see main started, so we’re at our entry point here. We’ve created t1, started t1, created t2, started t2, and t3. That’s why up here we’re seeing myfunc1 started, started, started.

02:43 And then we did our joins, so by doing .join() on each thread, we told the main thread to say, “Hey, wait up for these ones, wait for these to finish before continuing your execution.” So then we saw myfunc2 ended, myfunc1 ended, myfunc3 ended, and then we saw main ended.

03:05 You may notice, like, “Why did myfunc2() end before myfunc1() ended? myfunc1() started before myfunc2()!” Well, that’s the nature of multi-threaded applications.

03:18 That’s the nature of your CPU being in high demand and the operating system scheduling the execution of these threads. Sometimes it doesn’t come out in direct order—actually, most of the time it doesn’t, and you can’t rely on things finishing when you think they should have finished. But besides that, I just wanted to show you the repetitive nature here of using this explicit call to Thread.start() and Thread.join(), and when you have multiple threads and multiple functions, it results in very repetitive code. So in the next lesson, we’re going to learn about a very cool construct that allows us to simplify this process and create code that isn’t so repetitive.

Najmeh on March 4, 2020

Very useful tutorial. Thank you so much! :)

Become a Member to join the conversation.