Concurrency vs parallelism
Concurrency means juggling several tasks so that waiting time is used for other work; parallelism means doing several things at literally the same moment on different CPU cores. In Python, use threads or asyncio for I/O-bound work (waiting on networks and files) and multiple processes for CPU-bound work (heavy calculations).
A cooking example
Ten friends are coming for lunch and you have to wash vegetables, cut them, cook the curry and boil rice. Doing every step one after another is sequential. Putting the rice on to boil and cutting vegetables while it cooks is concurrency: one cook, but no idle waiting. Asking a friend to cook the curry on a second stove while you make the rice is parallelism: two cooks working at the same moment.
| Approach | Kitchen | Computer |
|---|---|---|
| Sequential | One cook, one step at a time | One task finishes before the next starts |
| Concurrent | One cook, switches while rice boils | Threads or asyncio switch while waiting |
| Parallel | Several cooks, several stoves | Several processes on several CPU cores |
CPU, cores, processes and threads
- A CPU has several cores; each core can run one thing at a time.
- A process is a running program with its own memory. Every Python process has its own interpreter.
- A thread is a line of work inside a process. Threads share the process's memory, which makes them light but means they can step on each other's data.
import os
import threading
print("CPU cores reported:", os.cpu_count() is not None)
print("Current thread:", threading.current_thread().name)CPU cores reported: True Current thread: MainThread
I/O-bound vs CPU-bound
| Kind of work | What the program waits for | Examples | Use |
|---|---|---|---|
| I/O-bound | Network, disk, databases | Calling APIs, downloading files, querying a database | Threads or asyncio |
| CPU-bound | Nothing — the CPU is busy | Image processing, number crunching, parsing huge files | Processes |
import time
def io_task():
time.sleep(0.2) # waiting: the CPU is idle
def cpu_task():
return sum(i * i for i in range(300_000)) # CPU is busy
for task in (io_task, cpu_task):
task()
print(task.__name__, "done")io_task done cpu_task done
The GIL
The standard Python interpreter (CPython) has a Global Interpreter Lock: only one thread runs Python code at any moment. Threads still help with I/O because a thread releases the GIL while it waits, but they don't speed up pure Python calculations. Separate processes each have their own GIL, so they do run in parallel.
Choosing a tool
| Situation | Tool | Lesson |
|---|---|---|
| A few dozen API calls or downloads | ThreadPoolExecutor | Threads |
| Thousands of network connections | asyncio | async and await |
| Heavy calculations on many items | ProcessPoolExecutor | Multiprocessing |
| Numbers in arrays | NumPy (already uses fast C code) | NumPy for AI |
Practice
Write best_tool(task: str) -> str that returns "threads" for "download" and "api call", "processes" for "resize images" and "train model", and "sequential" for anything else. Print the result for each of: download, train model, add two numbers.
threads processes sequential