Learn / Advanced / Lesson 1

Concurrency vs parallelism

11 min Tested on Python 3.15Updated By
In short

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.

ApproachKitchenComputer
SequentialOne cook, one step at a timeOne task finishes before the next starts
ConcurrentOne cook, switches while rice boilsThreads or asyncio switch while waiting
ParallelSeveral cooks, several stovesSeveral 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.
Python
import os
import threading

print("CPU cores reported:", os.cpu_count() is not None)
print("Current thread:", threading.current_thread().name)
Output
CPU cores reported: True
Current thread: MainThread

I/O-bound vs CPU-bound

Kind of workWhat the program waits forExamplesUse
I/O-boundNetwork, disk, databasesCalling APIs, downloading files, querying a databaseThreads or asyncio
CPU-boundNothing — the CPU is busyImage processing, number crunching, parsing huge filesProcesses
Python
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")
Output
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

SituationToolLesson
A few dozen API calls or downloadsThreadPoolExecutorThreads
Thousands of network connectionsasyncioasync and await
Heavy calculations on many itemsProcessPoolExecutorMultiprocessing
Numbers in arraysNumPy (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.

Expected output:
threads
processes
sequential
Your code

Quick check

1. Calling 50 web APIs is mostly...

2. Why don't threads speed up pure Python number crunching in standard CPython?

3. Parallelism means...

Use AI at work

Python is the base of AI. On AIMates you learn to use AI tools well in your daily job.

Visit AIMates →