thread
In Python, a thread is an independent flow of execution within a single process. With threads, a program can make progress on multiple tasks concurrently. For example, keeping a user interface (UI) responsive while performing network requests in the background.
Threads are often most helpful for I/O-bound tasks or high-latency work, such as file operations, network calls, or waiting on external services where they can improve responsiveness and overall throughput.
Python’s standard library includes the threading module, which provides tools for creating, coordinating, and managing threads.
One important caveat when using threads in CPython could be the Global Interpreter Lock (GIL). The GIL ensures that only one thread executes Python bytecode at a time, which means threads typically don’t speed up CPU-bound task.
Example
Here’s a quick example of how to create and use a thread in Python:
>>> import threading
>>> import time
>>> def print_numbers():
... for i in range(5):
... print(i)
... time.sleep(1)
...
>>> # Create a thread
>>> thread = threading.Thread(target=print_numbers)
>>> # Start the thread
>>> thread.start()
>>> # Main thread continues to run concurrently
>>> for i in range(5, 10):
... print(i)
... time.sleep(1)
...
>>> # Wait for the worker thread to finish
>>> thread.join()
0
5
1
6
2
7
8
3
4
9
In this example, print_numbers() runs in a separate thread while the main thread prints its own numbers. The output from both threads will interleave, demonstrating concurrent execution.
Related Resources
Tutorial
An Intro to Threading in Python
In this intermediate-level tutorial, you'll learn how to use threading in your Python programs. You'll see how to create threads, how to coordinate and synchronize them, and how to handle common problems that arise in threading.
For additional information on related topics, take a look at the following resources:
- Python Thread Safety: Using a Lock and Other Techniques (Tutorial)
- Python 3.13: Free Threading and a JIT Compiler (Tutorial)
- Threading in Python (Course)
- Python Threading (Quiz)
- Thread Safety in Python: Locks and Other Techniques (Course)
- Python Thread Safety: Using a Lock and Other Techniques (Quiz)
- Python 3.13: Free Threading and a JIT Compiler (Quiz)
By Leodanis Pozo Ramos • Updated Jan. 26, 2026