Advanced Python
Threads, Processes, Concurrency & Parallelism
Key Definitions
| Term | Definition |
|---|---|
| Process | Any program currently executing on your computer (e.g. Chrome, VS Code, Word) |
| Thread | A unit of work that makes up a process; every process has at least one |
| Physical core | An actual hardware core built into the CPU |
| Logical core | A virtual core created by hyper-threading; each physical core = 2 logical cores |
| Concurrency | Multiple threads in a program, executed one at a time (taking turns) |
| Parallelism | Multiple threads executing at the exact same time across multiple CPU cores |
Processes
- A process is any running program on your computer
- A single application may consist of multiple processes
- Your CPU shares its workload across all running processes
- Examples of processes running simultaneously: Chrome, Slack, VS Code, recording software
Threads
- A thread is a unit of execution within a process
- Every process has at least one thread
- A process can be split into multiple threads for better performance
- The CPU works on individual threads — not directly on processes
Process
├── Thread 1
├── Thread 2
└── Thread 3
CPU Architecture
Physical Cores
- The actual processing units built into the CPU hardware
- The number of physical cores = number of operations that can happen simultaneously
- Common counts: 2, 4, 6, 8, up to 64+ cores
Logical Cores (Hyper-Threading)
- A technology (common in Intel CPUs) where each physical core acts as 2 logical cores
- Each physical core can execute 2 operations at the same time
- Logical cores = physical cores × 2
4 physical cores → 8 logical cores (with hyper-threading)
How many threads can run at once?
The number of logical cores = the number of threads that can execute in parallel at any moment
How the CPU Manages Threads
- The CPU is always working — if one thread stalls (waiting for a file, network, etc.), it immediately switches to another thread
- Managing which threads run when is called scheduling — handled automatically by the CPU and OS
- As a programmer, you decide how to split your program into threads
Concurrency
One thread executing at a time, but multiple threads exist and take turns.
Thread A: ──────────── (stalls)
Thread B: ──────────── (stalls)
Thread A: ────────────
- Requires only one logical core
- Threads don't run simultaneously — they alternate
- When one thread stalls, the CPU switches to another
Practical example — saving a file while using the UI
| Without concurrency | With concurrency |
|---|---|
| Saving a file freezes the entire UI | Save thread stalls → CPU switches to UI thread |
| User can't interact until save finishes | User can keep using the app while saving |
Parallelism
Multiple threads executing at the exact same time across multiple logical cores.
Core 1: Thread A ──────────────────────
Core 2: Thread B ──────────────────────
- Requires multiple logical cores
- Threads don't wait for each other — they truly run simultaneously
- Example: Thread A prints 1, 2, 3, 4 while Thread B prints 5, 6, 7, 8 at the same time → output could be
1 5 2 6 3 7 4 8 - More powerful than concurrency but more complex to implement correctly (synchronisation required)
Concurrency vs Parallelism
| Concurrency | Parallelism | |
|---|---|---|
| Threads at same time | 1 (taking turns) | Multiple (truly simultaneous) |
| Cores needed | 1 logical core | Multiple logical cores |
| Thread interaction | One waits for another | Both run independently |
| Complexity | Simpler | More complex (synchronisation needed) |
| Best for | I/O-bound tasks (file, network waits) | CPU-bound tasks (heavy computation) |
Key Takeaways & Recap
| Concept | Summary |
|---|---|
| Process | A running program; made up of one or more threads |
| Thread | A unit of execution within a process |
| Physical core | Hardware processing unit on the CPU |
| Logical core | Virtual core via hyper-threading (physical × 2) |
| Concurrent program | Multiple threads, one executes at a time — threads take turns |
| Parallel program | Multiple threads execute at the exact same time on multiple cores |
| CPU scheduling | The CPU automatically switches between threads to stay busy |