Multithreaded programming in Python

Coding with Python

🕑 This lesson will take about 20 minutes

In this lesson, you will learn about multithreaded programming in Python. Multithreading is a programming concept where the application can have a small unit of tasks that can be executed in parallel (at the same time). Web servers typically have multiple threads running concurrently so that the application can serve multiple clients/users at the same time.

Running several threads is kind of like running several different programs at the same time. However, multithreading provides the following benefits:

  • Multiple threads within a single process use the same data space as the main thread, meaning they can share information or communicate with each other more easily than if they were completely separate processes

  • Threads do not require much memory overhead, and are cheaper than separate processes

Threads have a beginning, an execution sequence, and a conclusion. Each thread has an instruction pointer that keeps track of where it is currently running within its context:

  • It can be pre-empted (interrupted)

  • It can temporarily be paused (also known as sleeping or putting on hold) while other threads are running - this is called yielding.

There are two different kinds of threads: Kernel Threads which are a part of the operating system, and user threads which are not implemented in the kernel.

In this tutorial, you will learn how to use the threading module in Python to implement multithreading.

Creating a new thread

In the simple example below, we create a new thread and then start the thread by calling the start() method. The function called myFunction() will then execute and display "Sleeping 5 seconds from Thread 1". It will then sleep for 5 seconds, while the rest of the program continues running, displaying the "Hello, world!" message. Then, 5 seconds later, the "Awake from Thread 1" message will be displayed.

Note that you must import both the threading and time modules.

The output from this program will be:

"Sleeping 5 seconds from Thread 1"
"Hello, world!"

and then 5 seconds later…

"Awake from Thread 1"

Creating and running multiple threads

In this example, two threads are created that each count from 1 to 5. As they count from 1 to 5, they print their progress along the way. Both threads start at the same time, count from 1 to 5 at the same time, and finish running at the same time.

Note how when they threading.Thread() function is called, two arguments are provided - the target function to call (eg. the count_to_five() function) and a list of arguments (args) that can be passed into the function - in this case the thread name is passed into the function.

In this example, the output will be:

Both threads are starting...

Thread 1: 1 sec.
Thread 2: 1 sec.
Thread 2: 2 sec.
Thread 1: 2 sec.
Thread 2: 3 sec.
Thread 1: 3 sec.
Thread 2: 4 sec.
Thread 1: 4 sec.
Thread 2: 5 sec.
|Thread 1: 5 sec.

Both threads have finished!