Threading in Python
What is a thread?
A thread is a unit of execution inside a process.
- One process can have multiple threads.
- Threads share the same memory of the process.
When to use threading
Threading is best for I/O-bound tasks:
- network requests
- reading/writing files
- waiting on APIs
When NOT to use threading
For CPU-bound tasks (heavy computation), Python threads are limited by the GIL (Global Interpreter Lock) in CPython.
For CPU-bound parallelism, prefer:
multiprocessingmultiprocessing- native extensions
The Global Interpreter Lock (GIL)
In CPython, only one thread executes Python bytecode at a time.
Practical takeaway:
- Threads can still improve performance when time is spent waiting (I/O).
- Threads wonβt speed up CPU-only loops significantly.
Quick demo (I/O wait simulation)
io_wait_demo.py
import time
import threading
def task(i: int) -> None:
time.sleep(1)
print("done", i)
threads = []
start = time.time()
for i in range(5):
t = threading.Thread(target=task, args=(i,))
t.start()
threads.append(t)
for t in threads:
t.join()
print("seconds:", round(time.time() - start, 2))io_wait_demo.py
import time
import threading
def task(i: int) -> None:
time.sleep(1)
print("done", i)
threads = []
start = time.time()
for i in range(5):
t = threading.Thread(target=task, args=(i,))
t.start()
threads.append(t)
for t in threads:
t.join()
print("seconds:", round(time.time() - start, 2))Without threads, this would take about 5 seconds; with threads itβs ~1 second (because sleep is I/O wait).
π§ͺ Try It Yourself
Exercise 1 β Create a Simple Thread
Exercise 2 β Join a Thread
Exercise 3 β Run Multiple Threads
If this helped you, consider buying me a coffee β
Buy me a coffeeWas this page helpful?
Let us know how we did
