Signup/Sign In

Introduction to Multithreading In Python

In this tutorial we will introduce you to the concept of Multithreading and how threads can be implemented in python programming language. So let's start with understanding what threads are.


Threads

Threads are lightweight processes (subparts of a large process) that can run concurrently in parallel to each other, where each thread can perform some task. Threads are usually contained in processes. More than one thread can exist within the same process. Within the same process, threads share memory and the state of the process.


Types Of Thread

There are two kinds of thread:

  • Kernel-level threads
  • User level threads

Below we have explained a few differences between the two:

Kernel-level threadsUser level Thread
Recognized by the operating system. Does not recognized by the operating system.
Implemented by the operating system. Implemented by a user of the system.
Implementation is complex. Implementation is simple and easy.
Solaris is an example Posix is an example
Requires hardware support Requires no hardware support

What is Multithreading?

Now that we have a basic idea about what threads are, let's try to understand the concept of Multithreading.

Modern computers have a CPU that has multiple processing cores and each of these cores can run many threads simultaneously which gives us the ability to perform several tasks concurrently. This process of running multiple Threads concurrently to perform tasks parallely is called Multithreading.

Multithreading provides the following benefits:

  • Multiple threads within a process share the same data space and can, therefore, share information or communicate with each other more easily than if they were separate processes.
  • Threads do not require much memory overhead; they are cheaper than processes in terms of memory requirements.
  • Multithreaded programs can run faster on computer systems with multiple CPUs because these threads can be executed concurrently.

Time for an Example

Let's say you create a simple application for an Event's registration where attendees must register if they wish to attend the event. You have a simple HTML form for the users to fill in, and a backend which is a single threaded application.

As the application is single threaded, it can only process one request at a time. But what if the event is a "Coldplay's Music Concert" where millions of people want to register. Processing one request at a time will drastically slow down the performance.

So, we make the application multi-threaded and start multiple threads inside it hence allowing parallel processing.

Multithreading python example


Multithreading in Python

For performing multithreading in Python threading module is used.The threadingmodule provides several functions/methods to implement multithreading easily in python.

Before we start using the threading module, we would like to first introduce you to a module named time, which provides a time(), ctime() etc functions which we will be using very often to get the current system time and another crucial function sleep() which is used to suspend the execution of the current thread for a given number of seconds.

For example,

Now let's see how we can use the threading module to start multiple threads.

Code Example:

Let's try to understand the above code:

We imported the thread class using import threading statement and we also imported the time module. To create a new thread, we create an object of te Thread class. It takes the following arguments:

target: The function which will be executed by the thread.

args: The arguments to be passed to the target function. We can pass multiple arguments separated by comma.

In the example above, we created 2 threads with different target functions i.e. thread1(i) and thread2(i).

To start a thread, we have used the start() method of the Thread class.

We have also used the time.sleep() method of the time module to pause the execution of thread1 for 3 seconds.

Once the threads start, the current program (you can think of it as the main thread) also keeps on executing. In order to prevent the main program from completing its execution until the thread execution is complete, we use the join() method.

As a result, the current program will wait for the completion of t1and t2 and only after their execution is finished, the remaining statements of the current program will get executed i.e the statement print('Execution completed.').

You should try running the above code once after commenting out the code on line number 16,17 where we use the join method and see the result.

So with this our basic introduction to multithreading in python is completed and now we know how to create and start multiple threads in python.