Lesson · Concurrency Fundamentals
Processes, Threads & Tasks
81 min readProcesses, Threads & Tasks
Processes, threads, tasks, and interpreters are different concurrency units with different isolation and scheduling models. Choosing among them starts with CPU-bound versus I/O-bound work and shared-state needs.
Learning goals
- Compare a process, a thread, an asyncio task and an interpreter by what memory they share and what they isolate.
- Classify a workload as CPU-bound or I/O-bound, and pick the concurrency unit that fits it.
- Explain why concurrency is not the same as parallelism, with one example of each.
- Run work on a separate interpreter with
interpreters.create()andinterp.call(...), and say what crosses that boundary explicitly. - Decide when the communication cost of processes or interpreters is worth paying over threads' cheap shared memory.
Mental model
Two questions order the choice: what the work spends its time on, and what state it has to share. More isolation makes every crossing explicit and costs serialization or start-up time, while shared memory stays cheap but needs synchronization to keep its invariants.
Mental model
read it top to bottom
first question: what does the work wait on? I/O (files, sockets, databases) threads or asyncio overlap the waiting CPU (pure computation) processes or interpreters add real cores mixed split the stages, choose per stage second question: what state must be shared? little to share isolation is cheap, boundaries stay explicit lots of mutable state shared memory stays cheap, synchronization keeps it safe
Version note
Python 3.14 adds concurrent.interpreters to the standard library, making multiple interpreters directly accessible from Python.
Core idea
Every concurrency choice comes down to two axes: how much state the units share, and whether the work needs overlapping waits or real parallel execution. I/O-bound work delegates its waiting well to threads or asyncio, while CPU-bound work needs processes or multiple interpreters to gain parallel speed-up. Sharing memory is the cheapest communication and the most dangerous, so the more isolation a unit provides, the more explicitly data has to cross a boundary.
Process
A process has its own operating-system address space and resources. Separate processes provide strong isolation but require explicit communication.
Thread
A thread is an execution stream inside a process and therefore shares that process's memory. Shared memory makes communication cheap and synchronization necessary.
Coroutine/task
A coroutine is a suspendable computation created by an async def call; an asyncio Task schedules such a coroutine on an event loop. Tasks share one process and usually one event-loop thread.
CPU-bound work
CPU-bound work spends most of its time executing computations rather than waiting for external resources. Parallel execution strategy matters because scheduling overhead can easily outweigh small tasks.
I/O-bound work
I/O-bound work spends significant time waiting for files, sockets, databases, or other external systems. Threads or asyncio can overlap those waits effectively.
Concurrency vs parallelism
Concurrency is about managing multiple in-progress tasks, while parallelism means work actually executes at the same time. A program can be concurrent without being parallel.
Multiple interpreters as a concurrency model (Python 3.14)
Python 3.14 exposes multiple interpreters through the standard library. Each interpreter has isolated runtime state, which can simplify reasoning and enable multi-core execution when combined with threads.
concurrent.interpreters overview
concurrent.interpreters manages isolated interpreters in one process. Communication is explicit, and third-party extension compatibility must be checked.
Isolation differences among threads, interpreters, and processes
In Processes, Threads & Tasks, Isolation differences among threads, interpreters, and processes names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Python 3.14 example: multiple interpreters
An interpreter is an isolated execution context. It does not create concurrency by itself; concurrency comes from combining interpreters with threads or executor abstractions.
from concurrent import interpreters interp = interpreters.create() def double(value): return value * 2 print(interp.call(double, 21))
Common mistakes
- Choosing the unit before reading the workload: a model picked for the wrong bottleneck either serializes CPU work that needed cores or pays process start-up for waits that threads could have overlapped for free.
- Assuming concurrency improves throughput by itself: every unit adds scheduling, synchronization and communication, so work that is too small to cover those costs runs slower than the sequential version.
- Ignoring isolation and communication costs: crossing a boundary serializes data and copies state, so moving a large object graph between processes can cost more than the work it parallelizes.
- Treating threads and tasks as interchangeable: threads are preempted at arbitrary points while tasks yield cooperatively at awaits, so the same shared state needs different reasoning in each model.
Practice: Easy
- Run the comparison loop and, for each of the four rows, name one real task (a download, a batch of calculations, a scheduled job) that fits that unit.
- Time
sum(range(5_000_000))in the REPL, classify it as CPU-bound or I/O-bound from the numbers, and name the unit you would try first for a hundred copies of it. - Run the interpreters example and state whether
doubleexecutes with the main interpreter's globals or its own, using the isolation column of the table as evidence.
Practice: Medium
- Run eight
time.sleep(0.2)calls sequentially and then throughThreadPoolExecutor(max_workers=8), record both wall-clock times, and explain the gap with the I/O-bound idea. - Take a CPU-heavy loop, run it once directly and once across two
interpreters.create()handles, then explain the result with the isolation costs from the concept guide. - Model the same job as a thread with a shared
listand as two interpreters exchanging results explicitly; list what each version can see of the other's state.
Practice: Hard
- Design a decision table for a new feature: rows for CPU-bound, I/O-bound and mixed work, columns for how much mutable state must be shared, and the unit you would choose in each cell; justify three cells with the costs named in this lesson.
- Build a failure lab: a thread-heavy job where nothing is joined, more workers than the machine can hold at once, and mutable state passed across a process boundary without a plan; record what you observe in each case and tie it to the unit's isolation model.
- Take a mixed workload (read files, then parse them) and split it into stages: choose an I/O unit for the read stage and a CPU unit for the parse stage, then mark where data must be serialized between them and why the split beats one unit for both stages.
Lesson checklist
- I can explain the isolation difference between a thread, a process, an asyncio task and an interpreter.
- I can classify a workload as CPU-bound or I/O-bound and name a unit that fits it.
- I can say why concurrency is not the same as parallelism, with one example of each.
- I can run work through
interpreters.create()and describe what crosses that boundary explicitly.
Remember
Concurrency units differ in two ways that matter: whether they can execute in parallel and how much state they share. Sort the workload first (waiting versus computing) and the sharing second, and the right unit usually names itself: threads and tasks for overlapping waits, processes and interpreters for parallel computation, with explicit communication as the price of isolation.
Interview angles
What is the main purpose of **Process**?+
A process has its own operating-system address space and resources. Separate processes provide strong isolation but require explicit communication.
What should you pay attention to when using **Thread**?+
A thread is an execution stream inside a process and therefore shares that process's memory. Shared memory makes communication cheap and synchronization necessary.
How does **Coroutine/task** fit into this lesson?+
A coroutine is a suspendable computation created by an async def call; an asyncio Task schedules such a coroutine on an event loop. Tasks share one process and usually one event-loop thread.
Why should the CPU-bound versus I/O-bound question come before choosing among processes, threads and tasks?+
The classification decides which costs are relevant: I/O-bound work benefits from overlapping waits and barely gains from extra cores while paying process overhead, whereas CPU-bound work needs units that can actually execute in parallel, so the wrong unit spends the costs without collecting the benefit.