You are currently viewing How to Use threading Module to Create Threads in Python

How to Use threading Module to Create Threads in Python

You may have heard the terms “parallelization” or “concurrency“, which refer to scheduling tasks to run parallelly or concurrently (at the same time) to save time and resources. This is a common practice in asynchronous programming, where coroutines are used to execute tasks concurrently.

Threading in Python is used to run multiple tasks at the same time, hence saving time and resources and increasing efficiency.

Although multi-threading can save time and resources by executing multiple tasks at the same time, using it in code can lead to safety and reliability issues.

In this article, you’ll learn what is threading in Python and how you can use it to make multiple tasks run concurrently.

What is Threading?

Threading, as previously stated, refers to the concurrent execution of multiple tasks in a single process. This is accomplished by utilizing Python’s threading module.

Threads are smaller units of the program that run concurrently and share the same memory space.

How to Create Threads Using the threading Module

Python provides a module called threading that provides a high-level threading interface to create and manage threads in Python programs.

Create and Start a Thread

A thread can be created using the Thread class provided by the threading module. Using this class, you can create an instance of the Thread and then start it using the .start() method.

A thread is created by instantiating the Thread class with a target parameter that takes a callable object in this case, the num_gen function, and an args parameter that accepts a list or tuple of arguments, in this case, 3.

This means that you are telling Thread to run the num_gen() function and pass 3 as an argument.

If you run the code, you’ll get the following output:

You can notice that the Statement section of the code has finished before the Thread did. Why does this happen?

The thread starts executing concurrently with the main program and the main program does not wait for the thread to finish before continuing its execution. That’s why the above code resulted in executing the print statement before the thread was finished.

To understand this, you need to understand the execution flow of the program:

  • First, the "Statement: Creating and Starting a Thread." print statement is executed.
  • Then the thread is created and started using thread.start().
  • The thread starts executing concurrently with the main program.
  • The "Statement: Thread Execution Finished." print statement is executed by the main program.
  • The thread continues and prints the output.

The thread and the main program run independently that’s why their execution order is not fixed.

join() Method – The Saviour

Seeing the above situation, you might have thought then how to suspend the execution of the main program until the thread is finished executing.

Well, the join() method is used in that situation, it doesn’t let execute the code further until the current thread terminates.

After creating and starting a thread, the join() method is called on the Thread instance (thread). Now run the code, and you’ll get the following output.

As can be seen, the "Statement: Thread Execution Finished." print statement is executed after the thread terminates.

Daemon Threads

Daemon threads run in the background and terminate immediately whether they completed the work or not when the main program exits.

You can make a daemon thread by passing the daemon parameter when instantiating the Thread class. You can pass a boolean value to indicate whether the thread is a daemon (True) or not (False).

A thread is created by instantiating the Thread class passing the daemon_thread function inside it and to mark it as a daemon thread, the daemon parameter is set to True.

The daemon_thread() function is an infinite loop that prints a statement, sleeps for one second, and then again prints a statement.

Now when you run the above code, you’ll get the following output.

You can see that as soon as the main program exits, the daemon thread terminates.

At the time when the daemon_thread() function enters the loop, the concurrently running main program exits, and the daemon_thread() function never reaches the next print statement as can be seen in the output.

threading.Lock – Avoiding Race Conditions

Threads, as you know, run concurrently in a program. If your program has multiple threads, they may share the same resources or the critical section of the code at the same time, this type of condition is called race conditions.

This is where the Lock comes into play, it acts like a synchronization barrier that prevents multiple threads from accessing the particular code or resources simultaneously.

The thread calls the acquire() method to acquire the Lock and the release() method to release the Lock.

First, a Lock is created using the threading.Lock() and store it inside the lock variable.

An empty string is created (data) for storing the information from both threads concurrently.

The read_file() function is created that reads the information from the sample.txt file and adds it to the data.

The lock_task() function is created and when it is called, the following events occur:

  • The lock.acquire() method will acquire the Lock immediately when the lock_task() function is called.
  • If the Lock is available, the program will execute the read_file() function.
  • After the read_file() function finished executing, the lock.release() method will release the Lock to make it available again for other threads.

Within the if __name__ == "__main__" block, two threads are created thread1 and thread2 that both runs the lock_task() function.

Both threads run concurrently and attempt to access and execute the read_file() function at the same time but only one thread can access and enter the read_file() at a time due to the Lock.

The main program waits for both threads to execute completely because of thread1.join() and thread2.join().

Then using the print statement, the information present in the file is printed.

As can be seen in the output, one thread at a time reads the file. However, there were two threads that’s why the file was read two times, first by thread1 and then by thread2.

Semaphore Objects in Threading

Semaphore allows you to limit the number of threads that you want to access the shared resources simultaneously. Semaphore has two methods:

  • acquire(): Thread can acquire the semaphore if it is available. When a thread acquires a semaphore, the semaphore’s count decrement if it is greater than zero. If the count is zero, the thread waits until the semaphore is available.
  • release(): After using the resources, the thread releases the semaphore that results in an increment in the count. This means that shared resources are available.

Semaphore is used to limit access to shared resources, preventing resource exhaustion and ensuring controlled access to resources with limited capacity.

In the above code, Semaphore is instantiated with the integer value of 2 which means two threads are allowed to run at the same time.

Three threads are created and all of them use the thread_task() function. But only two threads are allowed to run at the same time, so two threads will access and enter the thread_task() function at the same time, and when any of the threads releases the semaphore, the third thread will acquire the semaphore.

Using ThreadPoolExecutor to Execute Tasks from a Pool of Worker Threads

The ThreadPoolExecutor is a part of concurrent.features module that is used to execute multiple tasks concurrently. Using ThreadPoolExecutor, you can run multiple tasks or functions concurrently without having to manually create and manage threads.

The above code creates a ThreadPoolExecutor with a maximum of 4 worker threads which means the thread pool can have a maximum of 4 worker threads executing the tasks concurrently.

Four tasks are submitted to the ThreadPoolExecutor using the submit method with the square_num() function and various arguments. This will execute the function with specified arguments and prints the output.

In the end, the shutdown method is called, so that ThreadPoolExecutor shutdowns after the tasks are completed and resources are freed.

You don’t have to explicitly call the shutdown method if you create ThreadPoolExecutor using the with statement.

In the above code, the ThreadPoolExecutor is used with the with statement. When the with block is exited, the ThreadPoolExecutor is automatically shut down and its resources are released.

Both codes will produce the same result.

Common Function in Threading

The threading module provides numerous functions and some of them are explained below.

Getting Main and Current Thread

The threading module has a main_thread() and a current_thread() function which is used to get the main thread and the currently running thread respectively.

Because the main_thread() and current_thread() functions return a Thread object, threading.main_thread().name is used to get the name of the main thread and threading.current_thread().name is used to get the name of the current thread.

Monitoring Currently Active Threads

The threading.enumerate() function is used to return the list of Thread objects that are currently running. This includes the main thread even if it is terminated and excludes terminated threads and threads that have not started yet.

If you want to get the number of Thread objects that are currently alive, you can utilize the threading.active_count() function.


Getting Thread Id

Every thread running in a process is assigned an identifier and the threading.get_ident() function is used to retrieve the identifier of the currently running thread.


A thread is a smaller unit in the program that is created using the threading module in Python. Threads are tasks or functions that you can use multiple times in your program to execute concurrently to save time and resources.

In this article, you’ve learned:

  • What is threading and how do you create and start a thread
  • Why join() method is used
  • What are daemon threads and how to create one
  • How to Lock threads to avoid race conditions
  • How semaphore is used to limit the number of threads that can access the shared resources at the same time.
  • How you can execute a group of tasks using the ThreadPoolExecutor without having to create threads.
  • Some common functions provided by the threading module.

Reference –

πŸ†Other articles you might be interested in if you liked this one

βœ…Comparing the accuracy of 4 pre-trained deep learning models?

βœ…What are coroutines in Python and how do use them in asynchronous programming?

βœ…Async/Await in Python using the asyncio module?

βœ…How to structure a Flask app using Flask Blueprint?

βœ…Upload and display images on the frontend using Flask in Python.

βœ…How to connect the SQLite database with the Flask app using Python?

That’s all for now

Keep Coding✌✌