Threads and Processes
- The GIL. CPython’s GIL lets only one thread run Python bytecode at a time: threads overlap waiting, not computing.
- Threads for I/O. Threads shine when tasks mostly wait: downloads, disk, databases: because waiting releases the GIL.
- Processes for CPU. The multiprocessing module runs separate interpreters on separate cores: the honest route for CPU-heavy work.
asyncio and Async Programming
- async def and await. An async function pauses at each await, letting the event loop run other tasks while this one waits.
- Tasks. asyncio.create_task starts a coroutine running concurrently: gather collects many results at once.
- One loop, no blocking. Everything shares one event loop: a single blocking call inside async code freezes every task at once.
Choosing the Right Concurrency Model
- Match the bottleneck. Choose by bottleneck: asyncio for many connections, threads for a few blocking calls, processes for raw CPU.
- Shared state costs. Every concurrency model pays for shared state: queues and message passing stay saner than locks.
- Keep it boring. The fastest correct choice is often sequential code: reach for concurrency when measurement says so.
Before and after this level
- Before: Level 10: Types & Modern Python: Type Hints, Generics and Protocols, Static Type Checking.
- Next: Level 12: Testing: Unit and Integration Tests, Fixtures, Mocking and Test Design, Reliable Python Test Suites.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Python Level 11: Concurrency cover?
Threads and Processes, asyncio and Async Programming, Choosing the Right Concurrency Model: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 10: Types & Modern Python, which covers Type Hints, Generics and Protocols, Static Type Checking.