How to use Python threading – with example

  • Reading time:14 mins read

What is Python threading used for?

A thread is a set of operations that are set for execution by a computer. Threading speeds up the process of program execution by allowing us to run parts of a program concurrently.

 

Whenever we execute Python code the compiler would normally execute code one line after another this means that the main thread will remain blocked until the current execution is over. So threading is a way that we can execute multiple pieces of code at the same time in a non-blocking way.

The Python threading module is fundamental in thread creation. It provides an interface with functions for starting, stopping and performing various operations on threads.

 

python threading

Getting started with Python threading

In this article, we are going to explore how we can use thread to speed up code execution. We will start by importing the Python threading module from the standard library. We will also make use of the sleep() function provided by the time module.

The sleep function suspends the execution of the current thread for a given number of seconds. This will allow us to simulate a waiting time so that we can actually see the execution process.

 

Example with different arguments

Now suppose we call the function twice with a different argument this time and reduce the wait times to 2 seconds. We can notice that in both cases the thread wait pauses for 2 seconds before printing the second print statement. 

 

 

Using Python threading to execute the statements concurrently

We can utilize thread to make the code statements run at the same time. Using the constructor threading.Thread() we are going to create two threads namely t1 and t2.

Once we have created the threads we are going to pass them the target function(s) along with the argument(s) of the function(s) in a tuple. 

 

python threading

Activating a thread

Using the start() function we are going to start the activity of the threads. The start() function can only be called once in a thread.

A thread cannot start running until the start() function has been invoked. Finally, to make our threads function as intended we have also added the join() method to make sure that the main thread waits for the threads to finish.

 

So now when we execute the code above you will realize that our print statements are executed concurrently unlike in our earlier examples.

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.