Threads — and when not to use them

Waiting on many HTTP calls is a maybe. One CPU-bound pandas groupby is a no. CPython’s GIL means threads often do not speed pure Python maths.

Charts from numbers

A picture that a principal can read in five seconds.

You will be able to

  • Keep a spec green before talking about speed
  • Know threads may help waiting on many HTTP calls
  • Know CPython’s GIL often will not speed pure Python maths

A clock is cheaper than a rewrite

Threads do not magically speed a pandas groupby on one CPU-bound attendance table. ‘It felt slow’ is not a ticket. ‘Prefix print is 20ms, full sort is 4s’ is a ticket. Measure first.

CPython has a GIL: one bytecode thread at a time for much pure Python. Waiting on fifty weather HTTP calls with timeouts is a maybe for threads. One GST function does not need a pool. This sandbox cannot start threads; we practise spec-first and a row counter you could wrap in timeit on a laptop.

When not to go parallel

Shared dicts without a plan. GUI toolkits that are not thread-safe. A 200-row CSV. If the job is waiting on HTTP, a timeout and retries matter more than a pool. If the job is pandas on one machine, vectorised operations beat a loop, and processes beat threads for pure CPU — and you still need a test that the paise total still matches.

Words that matter

GIL
CPython’s lock — threads often do not speed pure Python maths.
CPU-bound
The machine is computing, not waiting on disk or network.
IO-bound
Waiting on HTTP, disk, or a USB meter — threads might help.

Common mistakes

Avoid: Starting threads because a blog post did.

Do this: Measure, keep the spec green, then pick vectorise, cache, or (maybe) processes.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — a correct total first

Quality is the spec. Speed is later. No threads needed.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — count work you would measure

A row counter is the start of a timing story.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Example program — Tests beat a rumour

If the total is wrong, a faster wrong is still wrong.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Spec first

Sum [2, 3, 5] and print PASS if the total is 10.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. Threads are a poor first fix when…
2. Before rewriting for speed…

Progress is stored in a browser cookie on this device.