How to Perform Multithreading in Python


In this article, we show how to perform multithreading in Python.

Multithreading allows a Python program to run multiple threads simultaneously during program execution.

Multithreading is advantageous in a program where you want separate threads in a program to all run at the same time. If a program only runs one thread at a time, only one process can be performed at a time.

So in order to do multithreading in Python, we make use of the Python threading module. The threading module allows us to create and start threads.

In the example below, we're going to create a simple Python program that allows for the multithreading for 4 different processes. This means that these 4 different processes are running all at the same time within our program.

Without multithreading, each of these processes would run one at a time.

This is shown in the code below.

So the first thing we must do is import the threading module, which allows us to create a multithreaded program in Python.

We import the time module, so that we can create a time delay in our program. This is important because it will help to illustrate how multithreading works in this program.

We then create a function, called threadingfunction(), which prints the statements, "Starting the thread" and "Ending the thread" with a 2-second delay between these two statements.

We then create an empty list named threads[]

We then have a for loop which has a range of 4, which means we will have 4 threads within our multithreaded program.

We create a variable, th, which we set equal to, threading.Thread(target= threadingfunction)

This creates a thread, which we will run the threadingfunction() function.

We then start the thread with the start() function.

We then attach this thread to the threads list we created.

By using the join() function with each thread in the list, threads, we make sure that all otehr calling threads are blocked until the thread whose joint() method is called terminates. Thus, the threads being executed won't be interrupted by another thread.

The output of this program is shown below.

Now let's go over this output.

This output is signficant and demonstrates multithreading in Python well.

So there are 4 separate threads in this program. 4 separate threads are all executing at the same time.

When we run this Python program, you can see that the statement, "Starting the thread", appears 4 times.

There then is a 2-second delay.

Then the statement, "Ending the thread", appears 4 times.

This all happens because all 4 are running at the same time. Thus, you can see all have the same starting statements appear at the same time, all have the same 2-second delay, and then all of the same ending statements at the same time.

If multithreading was not used, then you would see all of these occurring one at a time, one after the other.

If you run the following program below, this is one thread executing at a time.

Even though it says it's a threading function, it isn't, because we took out the threading functionality.

Below is the output of this program.

So you see the big difference.

There is no mulithreading, so the program iterates through the function 4 times from start to finish instead of running the program simultaneously on 4 different threads.

And this is how to perform multithreading in Python.

Related Resources

How to Extract All Files and Folders from a Zip File in Python

How to Read the Contents of a Zip File in Python

HTML Comment Box is loading comments...