Start here
A job is too slow. Your first idea may be to add threads. That is the wrong first step.
Speed work has an order. You will use it in this chapter:
- Measure, to find the slow part.
- Fix the algorithm and cache repeated work.
- Pick the cheapest concurrency model that fits the slow part.
- Move to many machines (a cluster) only if one machine cannot do the job.
Two words come first. Concurrency means many tasks are in progress, and one worker switches between them. Parallelism means many tasks run at the same instant.
Parallelism needs many CPU cores. A core is one part of the CPU that runs code. Concurrency works on one core.
Next, ask where the time goes. If the task waits, it is I/O-bound. I/O means input and output, such as network and disk.
If the task computes, it is CPU-bound.
A checksum is a short code made from file data. Two files with the same data give the same code.
The shop gives us one task of each kind. An API (application programming interface) is a service that your code calls over the network.
Look at the two tasks in this table:
| Task | Where the time goes | Kind |
|---|---|---|
| Fetch 6 order pages from a slow API | Waiting for the network | I/O-bound |
| Checksum each order file in a Python loop | Running Python code on the CPU | CPU-bound |
The kind of task decides the tool. A concurrency model is a way to run many tasks: threads, processes or asyncio.
