Multithreading in Python

Python 12 min min read Updated: Mar 09, 2026 Advanced
Multithreading in Python
Advanced Topic 5 of 10

Python Multithreading

Multithreading is a programming technique that allows a program to run multiple tasks simultaneously within a single process. In Python, multithreading is commonly used to improve performance for tasks such as file processing, network requests, background operations, and user interface responsiveness.

Instead of executing tasks one after another, multithreading allows multiple threads to run concurrently, making applications faster and more efficient.

What is a Thread?

A thread is the smallest unit of execution within a process. A program can contain multiple threads that share the same memory space but execute different tasks independently.

For example, a web browser can download files, render pages, and handle user input simultaneously using multiple threads.

Why Use Multithreading?

  • Improve application performance
  • Run multiple tasks concurrently
  • Handle background operations efficiently
  • Improve responsiveness in applications

The threading Module

Python provides a built-in module called threading to work with threads.

python import threading def print_numbers(): for i in range(5): print(i) thread = threading.Thread(target=print_numbers) thread.start()

This creates a thread that executes the print_numbers() function.

Running Multiple Threads

You can run multiple threads to perform tasks simultaneously.

python import threading def task1(): for i in range(3): print("Task 1") def task2(): for i in range(3): print("Task 2") t1 = threading.Thread(target=task1) t2 = threading.Thread(target=task2) t1.start() t2.start()

Both tasks run concurrently using separate threads.

Joining Threads

The join() method ensures that the main program waits until a thread finishes execution.

python import threading def task(): print("Thread running") thread = threading.Thread(target=task) thread.start() thread.join() print("Thread finished")

This ensures proper synchronization between threads.

Thread with Arguments

You can pass arguments to threads using the args parameter.

python import threading def greet(name): print("Hello", name) thread = threading.Thread(target=greet, args=("Alice",)) thread.start()

The Global Interpreter Lock (GIL)

Python has a mechanism called the Global Interpreter Lock (GIL) that allows only one thread to execute Python bytecode at a time. This means that multithreading is most beneficial for I/O-bound tasks such as network operations and file handling rather than CPU-intensive computations.

Thread Synchronization

When multiple threads access shared resources, synchronization is required to avoid conflicts. Python provides synchronization tools such as locks.

python import threading lock = threading.Lock() def safe_task(): lock.acquire() print("Thread is running safely") lock.release() thread = threading.Thread(target=safe_task) thread.start()

The lock ensures that only one thread accesses the critical section at a time.

Daemon Threads

Daemon threads run in the background and automatically terminate when the main program exits.

python import threading import time def background_task(): while True: print("Running in background") time.sleep(1) thread = threading.Thread(target=background_task, daemon=True) thread.start() time.sleep(3) print("Main program finished")

Real-World Example

Multithreading is commonly used when downloading multiple files simultaneously.

python import threading import time def download(file): print(f"Downloading {file}") time.sleep(2) print(f"{file} downloaded") files = ["file1", "file2", "file3"] threads = [] for f in files: thread = threading.Thread(target=download, args=(f,)) threads.append(thread) thread.start() for t in threads: t.join()

This program downloads multiple files concurrently.

Multithreading vs Multiprocessing

Feature Multithreading Multiprocessing
Execution Multiple threads within one process Multiple processes
Memory Shared memory Separate memory
Best For I/O-bound tasks CPU-bound tasks

Best Practices

  • Use threads for I/O-bound tasks.
  • Avoid race conditions using locks.
  • Use thread pools for managing multiple threads.
  • Keep thread operations lightweight.

Conclusion

Multithreading in Python allows programs to perform multiple operations concurrently, improving performance and responsiveness. Although the Global Interpreter Lock limits true parallel execution for CPU-bound tasks, multithreading remains extremely useful for I/O-heavy applications.

Understanding multithreading helps developers build scalable applications such as web servers, automation tools, and data processing systems.

In the next tutorial, we will explore Multiprocessing in Python and learn how Python achieves true parallel execution.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators