Master Python Multithreading: Avoid Common Pitfalls


What is Python Multithreading?

Multithreading in Python refers to the concurrent execution of more than one sequential set of instructions, or "thread", in a single program. In simpler terms, it's a way to make your Python programs perform multiple tasks at the same time. Threads in Python operate within the same memory space, and therefore, they are well-suited for sharing information and resources with each other.

The concept of threading is especially important in Python because of the Global Interpreter Lock (GIL), a mutex that protects access to Python objects. The GIL can be a bottleneck in CPU-bound and multithreaded code, making it essential to understand how Python threads work in relation to the GIL.

Python's threading module provides a powerful set of tools to create and manage threads. It enables you to write programs that execute multiple functions in parallel, improving the efficiency of your code, particularly for I/O-bound or network-bound operations.


Basics of Multithreading

What is a Thread?

A thread is the smallest unit of a CPU's execution in a program. In Python, you can think of a thread as a separate flow of execution. Multiple threads can run in parallel and perform tasks without interfering with each other, although they share the same program memory and resources.


Single-threaded vs Multi-threaded Programs

  • Single-threaded Programs: All tasks are managed by the main thread, making it simpler but potentially slower for I/O-bound or CPU-bound tasks.
  • Multi-threaded Programs: Multiple threads run in parallel, which can significantly improve the speed of I/O-bound operations. However, it introduces complexity such as thread synchronization.


Importance of Global Interpreter Lock (GIL) in Python

The Global Interpreter Lock (GIL) is a mutex that allows only one thread to execute in the interpreter at any given time. This means that even on multi-core systems, Python threads can't utilize multiple cores effectively, limiting the performance gains in CPU-bound tasks.


Let's say you have a CPU-bound task that involves calculating the square of each element in a large list:

import threading
import time

def calculate_squares(numbers):
    for n in numbers:
        n * n

arr = list(range(1, 100000))

# Single-threaded execution
start = time.time()
print("Single-threaded took: ", time.time() - start)

# Multi-threaded execution
start = time.time()
t1 = threading.Thread(target=calculate_squares, args=(arr[:50000],))
t2 = threading.Thread(target=calculate_squares, args=(arr[50000:],))



print("Multi-threaded took: ", time.time() - start)

You can see that the multi-threaded version is slower due to the GIL.

Single-threaded took:  0.004740715026855469
Multi-threaded took:  0.007574796676635742


When to Use Python Multithreading

  • I/O-bound Tasks: When your program spends a lot of time waiting for I/O operations such as reading files, downloading content, etc., multithreading can be very effective.
  • Concurrent Execution: When you need to execute multiple tasks independently at the same time.
  • Resource Sharing: When tasks need to share resources or data with minimal overhead.
  • Simpler Parallelism: Threads are generally easier to create and manage compared to processes.


Getting Started with Python Multithreading

Hello World: Your First Multithreading Program

Once you have a basic understanding of what threads are, the next step is to write a simple multithreaded Python program. Let's create a program that uses two threads to print "Hello" and "World" separately.

Basic Code Snippet

import threading

def print_hello():
    for _ in range(5):

def print_world():
    for _ in range(5):

if __name__ == "__main__":
    t1 = threading.Thread(target=print_hello)
    t2 = threading.Thread(target=print_world)



Explaining the Code

  • Importing the threading module: The first step is to import Python's built-in threading module.
  • Defining Functions: We define two functions print_hello() and print_world() that will run in separate threads. Each function prints a word five times.
  • Creating Threads: The threading.Thread class is used to create new threads. We initialize two thread objects (t1 and t2) and pass the functions we defined earlier as the target.
  • Starting Threads: t1.start() and t2.start() start the execution of print_hello and print_world in separate threads.
  • Joining Threads: t1.join() and t2.join() ensure that the main program waits for both t1 and t2 to complete their execution before it exits.

Running the Program and Understanding Output

When you run this program, you'll notice that "Hello" and "World" are printed. However, they might not be printed in the order you expect. That's because threads run concurrently, and the order in which they execute can be unpredictable.

Your output might look something like this:



Understanding the Core Concepts

Understanding core concepts is vital for effectively using Python multithreading. Below are some of the essential elements you need to know.


1. Thread Creation

Creating threads in Python is relatively straightforward, thanks to the threading module.

1.1 Using threading.Thread

The most common way to create a thread is by using the Thread class from the threading module. Here is an example:

import threading

def my_function():
    print("Thread is executing")

# Create a thread
thread = threading.Thread(target=my_function)

# Start the thread

# Wait for the thread to complete

print("Thread has completed")

In this example, a new thread is created to run my_function. start() initiates the thread, and join() ensures the main program waits for the thread to complete.

1.2 Using threading Functions

Python's threading module also offers lower-level functions like However, these are less commonly used and not recommended for most use-cases because they don't offer the object-oriented benefits and better abstraction provided by the Thread class.


2. Thread Lifecycle

Understanding the lifecycle of a thread is crucial for effective Python multithreading. The stages include:

  • New: The thread is created but not started yet.
  • Runnable: After calling start(), the thread is considered runnable but may or may not be running yet.
  • Running: The thread is executing.
  • Blocked: The thread is alive but currently waiting for an external condition to be met, such as a lock to be released.
  • Terminated: The thread has completed execution.


3. Daemon Threads

Daemon threads are background threads that automatically exit as soon as the main program is done with its execution. They are useful for tasks that run in the background and don't have critical importance, like garbage collection or background I/O.

Here's how to set a thread as a daemon thread in Python multithreading:

# Create a daemon thread
daemon_thread = threading.Thread(target=my_function, daemon=True)

# Start the daemon thread

# Since it's a daemon thread, the program may exit before the thread has completed

In this example, my_function will be executed in a daemon thread. Even if this thread is still running, the program may exit if all non-daemon threads (including the main thread) have completed their execution.


Thread Synchronization

When multiple threads access shared resources, it can lead to unpredictable behavior or data inconsistencies. To manage this, Python's threading module provides several synchronization primitives like Locks, Semaphores, Events, Conditions, and RLocks in Python multithreading.

1. Locks

A lock is the most basic synchronization primitive. It allows only one thread to access a resource at a time.

Example using Locks

import threading

lock = threading.Lock()

def print_numbers():
    for i in range(5):

thread1 = threading.Thread(target=print_numbers)
thread2 = threading.Thread(target=print_numbers)



Here, the lock.acquire() and lock.release() methods ensure that only one thread at a time can execute the print_numbers() function.

2. Semaphores

In Python multithreading a Semaphore allows a fixed number of threads to access a resource simultaneously.

Example using Semaphores

sem = threading.Semaphore(2)

def print_numbers():
    for i in range(5):

# Now two threads can run print_numbers simultaneously

3. Event

In Python Multithreading, an Event is a simple thread synchronization object that allows one thread to signal an event and other threads to wait for it.

Example using Event

event = threading.Event()

def wait_for_event():
    print("Waiting for event to be set.")
    print("Event has been set.")

def set_event():
    print("Setting event.")

waiter = threading.Thread(target=wait_for_event)
setter = threading.Thread(target=set_event)


4. Condition

In Python Multithreading, a Condition object provides more advanced ways to synchronize threads. It's often used for producer-consumer problems.

Example using Condition

condition = threading.Condition()
queue = []

def producer():
    with condition:
        for i in range(5):

def consumer():
    with condition:
        while queue:
            item = queue.pop(0)
            print(f"Consumed {item}")

prod = threading.Thread(target=producer)
cons = threading.Thread(target=consumer)


5. RLock (Reentrant Lock)

In Python Multithreading, an RLock (or reentrant lock) is a lock object that can be acquired multiple times by the same thread.

Example using RLock

rlock = threading.RLock()

def nested_locks():
    with rlock:
        print("Acquired the first lock.")
        with rlock:
            print("Acquired the second lock.")

nested_thread = threading.Thread(target=nested_locks)


Thread Communication

When you're working with multiple threads, it often becomes necessary for threads to communicate with each other. Python multithreading offers various ways to achieve this.

1. Queues

The queue.Queue class is a thread-safe queue implementation that can be used to share data between threads.

Example using Queues

import threading
import queue

def producer(q):
    for i in range(5):
        print(f"Produced {i}")

def consumer(q):
    while True:
        item = q.get()
        print(f"Consumed {item}")

q = queue.Queue()

prod_thread = threading.Thread(target=producer, args=(q,))
cons_thread = threading.Thread(target=consumer, args=(q,))



Here, one producer thread adds items to the queue, and a consumer thread removes items from it. The Queue class takes care of all underlying locking, making it an excellent choice for thread-safe data exchange.

2. Global Variables and Shared State

Global variables can also be used for thread communication, although this method is generally not recommended because it can make the code harder to understand and maintain when using Python multithreading.

Example using Global Variables

import threading

shared_var = 0
lock = threading.Lock()

def increment():
    global shared_var
    with lock:
        shared_var += 1
        print(f"Incremented to {shared_var}")

thread1 = threading.Thread(target=increment)
thread2 = threading.Thread(target=increment)



In this example, both threads modify the shared_var global variable. The lock ensures that the variable is accessed by one thread at a time.

3. Thread Local Data

Thread-local data are data whose values are thread-specific. To manage thread-local data, Python provides the threading.local class.

Example using Thread Local Data

import threading

local_data = threading.local()

def show_data():
        val = local_data.value
    except AttributeError:
        print("No value yet")
        print(f"Value = {val}")

def set_data(val):
    local_data.value = val

show_thread = threading.Thread(target=show_data)
set_thread = threading.Thread(target=set_data, args=(5,))



show_thread = threading.Thread(target=show_data)

In this example, the set_data function sets a value attribute on the local_data object. The show_data function then attempts to print this value. Since the value is thread-local, it won't be available to other threads unless set within those threads.


Advanced Topics

Advanced topics in Python multithreading can provide you with more tools and techniques to solve complex problems. Here's an overview of some of those topics:

1. Recursive Locks (RLock)

RLocks, also known as Recursive Locks, allow a thread to acquire an already-acquired lock, preventing a thread from deadlocking with itself.

Example using RLock

import threading

rlock = threading.RLock()

def recursive_locks(n):
    with rlock:
        print(f"Level {n}")
        if n > 0:
            recursive_locks(n - 1)

recursive_thread = threading.Thread(target=recursive_locks, args=(3,))

In this example, the thread acquires the rlock multiple times recursively, which would not be possible with a regular Lock.

2. Timers

A Timer starts its work after a delay. It's a subclass of Thread.

Example using Timers

from threading import Timer

def delayed_function():
    print("This function runs after 5 seconds.")

timer = Timer(5, delayed_function)

Here, delayed_function will be executed after a 5-second delay.

3. Barrier

A Barrier is used to block a specific number of threads until all participating threads have reached the barrier.

Example using Barrier

import threading

barrier = threading.Barrier(2)

def wait_on_barrier(name):
    print(f"{name} is waiting at the barrier.")
    print(f"{name} passed the barrier.")

thread1 = threading.Thread(target=wait_on_barrier, args=("Thread-1",))
thread2 = threading.Thread(target=wait_on_barrier, args=("Thread-2",))


Both threads will wait at the barrier and proceed only when both have reached it.

4. Using Context Managers for Locks

Context managers (with statements) can make lock acquisition and release syntactically cleaner.

Example using Context Managers

lock = threading.Lock()

with lock:
    print("Critical section of code.")

Here, the with statement ensures that the lock is acquired before the block is entered and released after the block is exited.

5. Exception Handling and Debugging

Exception handling in threads can be done similarly to regular Python code, but debugging can be a bit more complicated.

Example using Exception Handling

def may_throw_exception():
        raise ValueError("This is an error.")
    except ValueError as e:
        print(f"Caught exception: {e}")

except_thread = threading.Thread(target=may_throw_exception)

In this example, the thread catches and handles a ValueError.


Best Practices

Understanding best practices is crucial for writing maintainable, efficient, and bug-free multithreaded code. Here are some best practices for Python multithreading:

1. Avoiding Deadlocks

Deadlocks occur when two or more threads wait for each other to release a resource they need. Avoiding nested locks and using timeout parameters can help.

Example to Avoid Deadlocks

lock1 = threading.Lock()
lock2 = threading.Lock()

def avoid_deadlock():
    with lock1:
        print("Acquired lock1")
        with lock2:
            print("Acquired lock2")

def reverse_avoid_deadlock():
    with lock2:
        print("Acquired lock2")
        with lock1:
            print("Acquired lock1")

thread1 = threading.Thread(target=avoid_deadlock)
thread2 = threading.Thread(target=reverse_avoid_deadlock)



Here, the order of acquiring locks is consistent, avoiding a deadlock situation.

2. Minimizing Thread Contention

Reducing the time a thread spends in a locked state can minimize contention.

Example to Minimize Thread Contention

lock = threading.Lock()

def low_contention_task():
    # Do some computation
    with lock:
        print("Brief critical section")
    # Do more computation

thread = threading.Thread(target=low_contention_task)

3. Optimizing I/O Operations

Threads are particularly useful for I/O-bound operations.

Example to Optimize I/O Operations

import time

def io_bound_task():
    print("Fetching data...")
    time.sleep(2)  # Simulate I/O operation
    print("Data fetched")

thread = threading.Thread(target=io_bound_task)

Here, other threads can continue working while one thread is waiting for the I/O operation to complete.

4. Understanding GIL Limitations

Python's Global Interpreter Lock (GIL) means that even with threads, only one operation can execute at a time per process. For CPU-bound tasks, you may need to use multiprocessing instead of Python multithreading.

Example Demonstrating GIL Limitation

def cpu_bound_task():
    sum([i*i for i in range(1, 10000000)])

# This won't speed up with threads because of GIL
thread1 = threading.Thread(target=cpu_bound_task)
thread2 = threading.Thread(target=cpu_bound_task)



Here, despite using threads, the program won't speed up for CPU-bound tasks due to the GIL.


Performance Considerations

Python Multithreading can significantly impact the performance of your program, for better or worse. Here are some performance considerations to keep in mind:


Thread Overheads

Every thread comes with a certain amount of overhead, such as memory for stack space and CPU time for context switching. Creating too many threads can lead to resource exhaustion and degrade performance.

Example to Show Thread Overheads

import time

def trivial_task():
    x = 0
    for _ in range(100):
        x += 1

start_time = time.time()
threads = [threading.Thread(target=trivial_task) for _ in range(1000)]

for thread in threads:

for thread in threads:

end_time = time.time()

print(f"Time taken: {end_time - start_time} seconds")

This example creates 1000 threads to do a trivial task, which is inefficient and will take longer due to the overhead associated with thread creation and destruction.


Profiling and Benchmarking

Understanding where your program spends its time can help you optimize it better. You can use profiling tools to measure the execution time of various parts of your code.

Example using time for Profiling

def compute_task():
    sum([i*i for i in range(1, 10000000)])

def io_task():

# Profiling compute-bound task
start_time = time.time()
end_time = time.time()
print(f"Compute-bound task took {end_time - start_time} seconds")

# Profiling I/O-bound task
start_time = time.time()
end_time = time.time()
print(f"I/O-bound task took {end_time - start_time} seconds")

In this example, we're using Python's built-in time module to measure the time taken for a compute-bound task and an I/O-bound task. This will give you an idea of where to focus your optimization efforts.


Error Handling and Debugging

Error handling and debugging are critical aspects of Python multithreading. Because of the concurrent execution, debugging and handling errors in multithreaded programs can be more complicated than in single-threaded programs. Here's how you can go about it:

1. Handling Exceptions in Threads

When a thread encounters an unhandled exception, it terminates silently without affecting other threads. Therefore, it's crucial to handle exceptions within each thread to ensure that they don't fail silently.

Example Handling Exceptions in Threads

import threading

def may_throw_exception():
        raise ValueError("This is an error.")
    except ValueError as e:
        print(f"Caught exception: {e}")

thread = threading.Thread(target=may_throw_exception)

Here, the thread catches and handles a ValueError exception. This allows you to log the issue or take other appropriate actions.

2. Debugging Multithreaded Programs

Debugging multithreaded programs can be challenging because threads run concurrently. You can use debugging tools that support Python multithreading, set breakpoints, or even manually insert debugging statements.

Example for Debugging with Prints

lock = threading.Lock()

def debug_task():
    with lock:
        print("Thread entering critical section")
        # Your code here
        print("Thread exiting critical section")

thread = threading.Thread(target=debug_task)

In this example, debug print statements are added to help follow the program's flow.

3. Logging and Monitoring

Logging is crucial when it comes to tracking issues and understanding the behavior of a multithreaded application. Python’s built-in logging library is thread-safe, so it can be used to log information from multiple threads.

Example Using Python’s Logging Module

import logging


def logging_task():
    logging.debug("This is a debug message")"Informational message")
    logging.error("An error has occurred!")

thread = threading.Thread(target=logging_task)

Here, we use Python's built-in logging module to log messages from within a thread. The logging module takes care of handling the messages in a thread-safe way.


Comparison Multithreading Vs Multiprocessing Vs Async I/O

It's essential to understand how Python multithreading compares with other techniques like multiprocessing and asynchronous I/O. Each method has its advantages and disadvantages, and choosing the right approach can greatly impact the efficiency and complexity of your application.

Criteria Multithreading Multiprocessing Async I/O
Concurrency Model Thread-based Process-based Event-loop based
GIL Impact Yes (Limited by GIL) No N/A
Best for I/O-bound tasks CPU-bound tasks I/O-bound, Scalable
Memory Usage Lower Higher Lower
Exception Handling Complex Easier Moderate
Debugging Difficult Moderate Moderate
Data Sharing Easier Complex Moderate
Language Support Native Native Native or Libraries
Functionality Isolation No Yes No

When to Use Which?

  • Use Multithreading for I/O-bound tasks: When your program spends more time waiting for I/O operations like reading files, web scraping, or accessing a database, Python multithreading can be very beneficial.
  • Use Multiprocessing for CPU-bound tasks: When you have a computationally heavy task that doesn't require much I/O, multiprocessing is usually the better choice because it avoids the GIL and fully utilizes multiple CPU cores.
  • Use Async I/O for Scalable I/O-bound tasks: If you're building a network server or application that needs to handle many simultaneous I/O-bound tasks, async I/O can be more scalable than threading or multiprocessing.


Security Implications

Python Multithreading introduces several security considerations that you must take into account to ensure the integrity and confidentiality of your data and communications. Below are some key aspects:

1. Data Integrity in Multithreading

When multiple threads share resources or data, there is a potential for data corruption or unauthorized data access if not handled carefully. Therefore, it's crucial to use synchronization mechanisms to protect data integrity.

Example for Data Integrity

import threading

# Shared resource
shared_resource = 0

# Lock for ensuring data integrity
resource_lock = threading.Lock()

def increment_resource():
    global shared_resource
    with resource_lock:
        temp = shared_resource
        temp += 1
        shared_resource = temp

# Create threads
threads = [threading.Thread(target=increment_resource) for _ in range(100)]

# Start and join threads
for t in threads:

for t in threads:

print("Shared Resource Value:", shared_resource)

In this example, a lock is used to ensure that the shared resource is accessed by only one thread at a time, preserving data integrity.

2. Secure Communication Between Threads

Threads in the same process share memory space, so communication between them is typically secure within that process. However, when sensitive data is involved, you may want to minimize the time it spends in shared state or apply additional encryption or other security measures.

Example for Secure Communication

from cryptography.fernet import Fernet

# Generate encryption key and cipher
key = Fernet.generate_key()
cipher = Fernet(key)

# Shared encrypted resource
encrypted_resource = cipher.encrypt(b"Sensitive Data")

def secure_access():
    global encrypted_resource
    with resource_lock:
        # Decrypt, use, and re-encrypt the sensitive data
        decrypted = cipher.decrypt(encrypted_resource)
        print("Decrypted Data:", decrypted.decode())
        encrypted_resource = cipher.encrypt(decrypted)

# Create and start threads
threads = [threading.Thread(target=secure_access) for _ in range(3)]

for t in threads:

for t in threads:

In this example, sensitive data is stored encrypted, even in shared memory. Threads decrypt it only when necessary and re-encrypt it immediately after.


Top 10 Frequently Asked Questions (FAQ)

What is the Global Interpreter Lock (GIL) and how does it affect multithreading in Python?

The GIL is a mutex that allows only one thread to execute in the interpreter at a given time. It can make CPU-bound tasks slower in a multithreaded Python program. However, for I/O-bound tasks, multithreading can still be useful.

How can I avoid deadlocks in a multithreaded application?

Deadlocks can often be avoided by acquiring locks in a pre-defined order or by using timeout-based techniques like lock.acquire(timeout=...).

What's the difference between a daemon thread and a non-daemon thread?

Daemon threads are background threads that automatically exit as soon as the main program finishes executing. Non-daemon threads will keep the program running until they complete their task.

When should I use thread pooling?

Use thread pooling when you have a lot of small tasks to be performed but want to limit the number of threads used. The concurrent.futures.ThreadPoolExecutor is often used for this.

How can I share data between threads safely?

You can use thread-safe data structures like queue.Queue, or use locking mechanisms like threading.Lock to protect shared resources.

Is it possible to change thread priority in Python?

Python’s standard library doesn't provide a direct way to change thread priorities. This is generally managed by the operating system.

What are the risks of using global variables in a multithreaded application?

Global variables can be accessed by multiple threads, leading to possible data corruption if not handled carefully. Use locks to synchronize access to global variables.

Can I make a multithreaded GUI application in Python?

Yes, you can use multithreading in GUI applications to keep the user interface responsive. But make sure to only update the UI elements from the main thread.

How can I handle exceptions in a thread?

Exception handling within threads is similar to normal exception handling in Python. However, exceptions in a thread won’t propagate to the main thread, so you should catch and handle them inside the thread itself.

Are Python’s built-in data types like lists and dictionaries thread-safe?

Built-in data types like lists and dictionaries are not inherently thread-safe. If you need to access them from multiple threads, use a lock.


Summary and Key Takeaways

Recap of Core Concepts and Functions

  • What is a Thread: A thread is the smallest unit of a CPU's execution, and multiple threads can exist within the same process.
  • Global Interpreter Lock (GIL): A mutex that protects access to Python objects, but can hinder the performance of CPU-bound multithreaded programs.
  • Thread Creation: You can create threads using the threading.Thread class or by using functions within the threading module.
  • Synchronization Mechanisms: Locks, semaphores, events, and conditions are various mechanisms to synchronize thread activities.
  • Daemon Threads: Threads that run in the background and do not prevent the program from terminating.

Best Practices and Tips

  • Avoid Deadlocks: Always acquire locks in a pre-defined order or use timeout mechanisms.
  • Minimize Thread Contention: Make locks as granular as possible to minimize waiting time.
  • Understand the Limitations of GIL: For CPU-bound tasks, consider using multiprocessing instead.
  • Secure Your Threads: Use encryption and locks to secure sensitive data and maintain data integrity.


Additional Resources

Links to Official Documentation


Views: 92
Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment