Python Concurrency & Parallelism – Breaking Past the GIL with Threading, Multiprocessing, and Asyncio
Quick Answer: Concurrency means handling multiple tasks by switching between them, while parallelism means running tasks truly at the same time using multiple CPU cores. In Python, the GIL restricts multi-threaded CPU work, so we use multiprocessing for heavy math and asyncio or threading for waiting on I/O operations.
Imagine Meena runs a busy restaurant in Bengaluru serving 500 customers daily.
Some tasks require waiting for deliveries or network responses, while other tasks require intensive calculations like billing and tax compilation.
How do we make our Python applications handle waiting operations and heavy computations without freezing up?
1. Why Do We Need Concurrency and the GIL?
The problem of single-threaded waiting
By default, Python executes code line by line in a single thread. If your script waits for a database query or an API response, the entire program pauses until that response arrives.
Furthermore, Python has a built-in safety lock called the GIL.
GIL = a mechanism in CPython that allows only one thread to execute Python bytecode at any given time.
Figure 1: Choosing the correct concurrency tool based on workload type.
2. Handling I/O-Bound Tasks with Threading
I/O-bound task = an operation that spends most of its time waiting for external inputs like network requests, disk reads, or file downloads.
Because waiting releases the GIL in Python, the threading module allows multiple threads to wait concurrently without blocking execution.
Example 1: Using threading to fetch multiple data sources concurrently
This script creates multiple threads to simulate waiting for network responses.
import threading
import time
def fetch_data (source_name):
print($"Starting fetch from {source_name}...")
time.sleep (2 ) # Simulate network delay
print($"Finished fetch from {source_name}!")
if __name__ == "__main__":
t1 = threading.Thread (target=fetch_data, args=("Database",))
t2 = threading.Thread (target=fetch_data, args=("External API",))
t1.start ()
t2.start ()
t1.join ()
t2.join ()
print("All data fetches completed.")
Expected output
Starting fetch from Database... Starting fetch from External API... Finished fetch from Database! Finished fetch from External API! All data fetches completed.
Notice how both fetches start together and finish around the same time, saving valuable execution seconds.
3. Bypassing the GIL for CPU-Bound Tasks with Multiprocessing
CPU-bound task = an operation that requires heavy mathematical computation or data processing, utilizing the computer processor to its maximum limit.
Because the GIL prevents multiple threads from running CPU-bound code in parallel, we use the multiprocessing module to spawn separate Python processes, each with its own memory space and Python interpreter.
Example 2: Using multiprocessing for intensive math calculations
This script runs parallel processes across multiple CPU cores.
from multiprocessing import Process
import time
def heavy_calculation (number):
print($"Process {number} starting heavy math...")
total = 0
for i in range (10000000 ):
total += i
print($"Process {number} result: {total}")
if __name__ == "__main__":
p1 = Process (target=heavy_calculation, args=(1 ,))
p2 = Process (target=heavy_calculation, args=(2 ,))
p1.start ()
p2.start ()
p1.join ()
p2.join ()
print("All calculations finished.")
Expected output
Process 1 starting heavy math... Process 2 starting heavy math... Process 1 result: 49999995000000 Process 2 result: 49999995000000 All calculations finished.
4. Modern Asynchronous Programming with Asyncio
Asyncio = a library used to write concurrent code using the async and await syntax for high-performance network services.
Unlike threading, which relies on the operating system to switch between threads, asyncio uses cooperative multitasking within a single thread.
Example 3: Asynchronous tasks using asyncio
This script handles non-blocking execution using an event loop.
import asyncio
async def serve_table (table_number):
print($"Taking order for Table {table_number}...")
await asyncio.sleep (2 )
print($"Serving food to Table {table_number}!")
async def main ():
await asyncio.gather (
serve_table (1 ),
serve_table (2 ),
serve_table (3 )
)
if __name__ == "__main__":
asyncio.run (main ())
Expected output
Taking order for Table 1... Taking order for Table 2... Taking order for Table 3... Serving food to Table 1! Serving food to Table 2! Serving food to Table 3!
| Tool | Best Use Case | Bypasses GIL? |
|---|---|---|
threading |
I/O-bound tasks (network calls, file reads) | No |
multiprocessing |
CPU-bound tasks (heavy computations, image processing) | Yes (Separate processes) |
asyncio |
High-concurrency network services and APIs | No (Single-threaded event loop) |
The important terms to remember
- GIL = Global Interpreter Lock restricting Python threads to one core.
- Concurrency = Structuring tasks to run overlapping in time.
- Parallelism = Executing multiple computations simultaneously on separate CPU cores.
- Event Loop = The core engine in asyncio that manages task execution.
Design tip: Never use threads for heavy mathematical computations in Python, as the GIL will cause performance bottlenecks.
5. Common Mistakes to Avoid
1. Using threading for heavy CPU calculations
Expecting multi-threading to speed up number crunching in Python will fail due to the GIL.
Better approach: Use multiprocessing or native C-extensions like NumPy for CPU-bound tasks.
6. Interview Questions & Answers
Question 1: What is the Global Interpreter Lock (GIL) and why does Python use it?
What the interviewer expects: A clear explanation of memory management and thread safety in CPython.
Strong answer: "The GIL is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecodes at once. It simplifies CPython's memory management and garbage collection, but it means multi-threading cannot utilize multi-core processors for CPU-bound tasks."
What interviewers expect at different levels
- Junior developer: Knows when to pick threading versus multiprocessing.
- Senior developer: Understands asyncio event loops and GIL internals in CPython.
- Technical architect: Designs distributed microservices to scale beyond single-node bottlenecks.
7. Practice Exercises
Beginner: Concurrent file downloader
Write a script using threading to simulate downloading three files concurrently with simulated sleep delays.
Expected result: Prints start and finish messages for all three files efficiently.
Intermediate: Parallel prime number finder
Use multiprocessing.Pool to calculate prime numbers across a range in parallel across multiple CPU cores.
Hint: Use pool.map() to distribute work evenly among worker processes.
8. Frequently Asked Questions (FAQ)
Does Python 3 support true multi-core threading without the GIL?
Recent Python versions (such as PEP 703 initiatives) explore building a free-threaded Python build without the GIL, but standard distributions still rely on it.
Can I mix asyncio and multithreading?
Yes. You can run blocking functions in a separate thread pool using loop.run_in_executor() inside an asyncio application.
9. Next Steps
Now that you have mastered concurrency and parallelism, you are ready to explore file I/O operations and database persistence in the next section.