Concurrency — Threads, Processes & Async
🎓 Advanced · Topic 04
Concurrency — Threads, Processes & Async
Speed up programs by running work simultaneously. Understand the GIL, when to use threads vs processes vs async, and write non-blocking I/O with asyncio.
⏱ ~70 min
🔴 Advanced
⚡ Performance
The GIL & Choosing the Right Model
Python's Global Interpreter Lock (GIL) prevents true parallel thread execution. Choose the right tool based on your bottleneck:
threading
Best for I/O-bound tasks — network calls, file reads, database queries. GIL is released during I/O.
multiprocessing
Best for CPU-bound tasks — image processing, number crunching. Each process has its own GIL.
asyncio
Best for high-concurrency I/O — thousands of simultaneous network connections, event-driven servers.
Threading
Multiprocessing
asyncio — async / await
Single-threaded concurrency driven by an event loop. await suspends the current coroutine while waiting for I/O, allowing other coroutines to run.
💡 async all the way down
Once you use async/await, the calling code must also be async. Use asyncio.run() as the single entry point at the top level.