1.Concurrency vs parallelism
Concurrency 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.
Key features
- Concurrency: structuring work as multiple tasks that can be interleaved or overlapped
- Parallelism: actually executing multiple tasks at the same physical instant
- One CPU core can be concurrent (switching between tasks) but never parallel
- Multiple cores make true parallelism possible — concurrency does not require them
- Python threads give concurrency for I/O-bound work; true CPU parallelism needs processes
The four combinations, and what gets you there in Python
- 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
Command
# concurrency: tasks make progress by interleaving (threads, asyncio)
# parallelism: tasks run at the same instant (multiprocessing, multiple cores)Concurrency is interleaving multiple tasks; parallelism is running them at the same instant. Python threads: concurrency. Python processes: parallelism.