Concurrency vs parallelism
coreintermediateConcurrency is structuring a program to deal with multiple tasks by interleaving them — not necessarily at the same instant. Parallelism is actually running multiple tasks at the same instant, on separate CPU cores.
Think of it as
One cook working a stove, an oven, and a pan at once — checking each in turn, never truly doing two things in the same instant — is concurrency. Three cooks each working their own station at the same instant is parallelism. A single-core machine can be concurrent but never parallel; a multi-core machine can be both.
What we're doing: Show that two threads interleave (concurrency) rather than run at the literal same instant, by observing their print order overlap.
- 4
- Each thread runs the same worker function independently, sleeping between steps.
- 5
- time.sleep releases control, letting the OTHER thread run — this is what makes the output interleave.
thread-A: step 0
thread-B: step 0
thread-A: step 1
thread-B: step 1
thread-A: step 2
thread-B: step 2
both threads doneWhy this works: The two threads take turns running — thread-A prints, sleeps (releasing the GIL), thread-B gets a turn, and so on. Neither line is printed at the literal same instant; they are interleaved, which is exactly what concurrency means. Getting true simultaneous execution of CPU work needs separate processes, not threads.
Assuming Python threads give parallelism for CPU-bound work
Wrong
Better
What you see: No error — the code runs, but two CPU-bound threads finish in roughly the same time as one, because only one thread runs Python bytecode at a time under the GIL. See the GIL subsection for the measured numbers.
Why: Python threads give concurrency (interleaving), not parallelism, for CPU-bound Python code — the Global Interpreter Lock (GIL) lets only one thread execute Python bytecode at a time. Threads still help for I/O-bound work, because the GIL is released while waiting on I/O.
- Concurrency
- Multiple tasks in progress, interleaved
- Works even on a single CPU core
- Python threads/asyncio: this, not parallelism
- Parallelism
- Multiple tasks running at the same instant
- Requires multiple CPU cores
- Python: needs multiprocessing, not threading
The four combinations, and what gets you there in Python
Together
Remember: Concurrency is interleaving multiple tasks; parallelism is running them at the same instant. Python threads: concurrency. Python processes: parallelism.
See also: cpu bound vs io bound workloads · concurrency approaches overview · what the gil is

