Concurrency · 1 / 15 Theory

Concurrency, parallelism and the GIL

A sequential program does one thing after another. But many programs spend most of their time waiting: for a server, a disk, a database. While they wait, the CPU is idle. Concurrency means structuring a program so that several tasks make progress at the same time (or seem to), taking advantage of that idle time.

Concurrency is not parallelism

  • Concurrency: several tasks are *in progress* at once, even if only one advances at each instant (they take turns). It is a way of organizing the work.
  • Parallelism: several tasks *literally run at the same time*, on several CPU cores. It is a way of executing the work.

A cook who chops vegetables while the pasta boils is being concurrent. Two cooks, each with their own stove, are being parallel. A program can be concurrent without being parallel.

Two kinds of tasks

TypeBottleneckExamplesWhat helps
I/O-boundWaiting for dataWeb requests, reading files, database queriesThreads or asyncio
CPU-boundDoing calculationsImage processing, simulations, cryptographyProcesses (multiprocessing)

Python's tools

ToolWhat it isWhen to use it
threadingThreads: several flows inside the same process, sharing memoryI/O tasks with code that blocks
asyncioCoroutines in a single thread with an event loopHuge numbers of I/O tasks (networks, servers)
multiprocessingSeparate processes, each with its own interpreter and memoryHeavy calculation that must use several cores
concurrent.futuresA common interface over thread and process poolsDistributing tasks without managing threads by hand

The GIL

The usual implementation of Python (CPython) has the GIL (*Global Interpreter Lock*): a lock that lets only one thread run Python code at a time. Practical consequences: threads do help when tasks wait (while one thread waits for I/O it releases the GIL and another advances), but they do not speed up pure Python calculation. For that you use processes. (Recent Python versions include an experimental build without the GIL, but portable code should not depend on it.)

Amdahl's law

Even with many processors, there is always a part of the work that cannot be parallelized. If a fraction p of the work is parallelizable, with n processors the maximum speedup is 1 / ((1 - p) + p / n). As n grows it approaches the limit 1 / (1 - p): with p = 0.9 you will never go beyond 10 times faster.

def speedup(p, n):
    return 1 / ((1 - p) + p / n)

for n in (1, 2, 4, 8, 1000):
    print(n, round(speedup(0.9, n), 2))
# 1 1.0
# 2 1.82
# 4 3.08
# 8 4.71
# 1000 9.91

You compute it in Amdahl's law. And how tasks take turns inside a scheduler you simulate in Round-robin tasks.

A warning about this course

The exercise corrector runs in your browser, where real threads and processes cannot be created. That is why the exercises in this lesson simulate concurrency in a deterministic way, and the threading and asyncio examples in the following articles are marked as *not runnable here*: try them on your own computer.