Module 6: Concurrency, Parallelism, and Executors
Workload Classification and the GIL
Workload Classification and the GIL
Concurrency structures overlapping tasks; parallelism runs tasks simultaneously. In normal CPython, the GIL permits one thread to execute Python bytecode at a time, so workload shape determines the useful model.
Module: Module 6: Concurrency, Parallelism, and Executors
Core mental model
This lesson is built around four connected ideas. Read them as a decision framework, then prove each one with the walkthrough.
- IO-bound work spends time waiting and can benefit from threads or asyncio.
- Pure-Python CPU-bound work usually needs processes for multi-core execution.
- Some native extensions release the GIL, so measurements can change the choice.
- Concurrency introduces scheduling, coordination, and failure complexity.
Walkthrough
from time import perf_counter, sleep
def io_task(name):
start = perf_counter()
sleep(0.2)
return name, perf_counter() - start
def cpu_task(limit):
return sum(number * number for number in range(limit))
print(io_task("service"))
print(cpu_task(100_000))sleep represents waiting that another thread or task could overlap. cpu_task repeatedly executes Python operations and is constrained by the interpreter lock when moved to ordinary threads.
Hands-on lab
Classify ten realistic tasks from your work as IO-bound, CPU-bound, mixed, or too small to parallelize. Propose a model and a measurement that could disprove each choice.
What to watch for
- The GIL does not make threads useless for waiting-heavy work.
- Concurrency is not automatically faster for tiny tasks.
- Adding workers can overload a database, API, disk, or memory limit.
Engineering checklist
- Connect the lesson's mental model to the choices made in the walkthrough.
- Test the normal case, an empty or boundary case, and one invalid case.
- Keep external input, side effects, and reusable logic in clearly separated layers.
- Prefer the clearest correct implementation before optimizing or generalizing it.
Completion check
You can implement the lab without copying the example, explain the core concepts in your own words, and show tests or terminal output that demonstrate the expected and failure paths.