Thread pool
medium 70 minA few threads that live long, one queue with a limit, and a loop: take, run, repeat.
Watch lesson5:06
Overload
Four answers to a full queue, as a strategy
The point: a full queue is not an error of the pool. It is the moment where the design decides who pays: the new task, the old one, or the caller.
Who pays?
| Policy | Pays | Java name | Danger |
|---|---|---|---|
| Refuse | The new task | AbortPolicy, the default | The caller ignores it |
| Caller runs | The caller's time | CallerRunsPolicy | It runs in an event loop or with a lock held |
| Drop the oldest | The one who waited longest | DiscardOldestPolicy | Its future never ends |
| Drop the new one silently | The new task, without knowing | DiscardPolicy | Data is lost and nobody sees it |
| Wait a moment | The caller's time, limited | write it yourself | Many callers wait together |
Policies as a strategy
Each answers one question: the queue is full, what now? The pool does not know which one it has. See strategy.
import threadingfrom collections import dequeclass Rejected(Exception): passclass Abort: def handle(self, box, job): raise Rejected("the queue is full")class CallerRuns: def handle(self, box, job): if box.closed: raise Rejected("the pool is closed") job() # in the thread that submitted itclass DropOldest: def handle(self, box, job): while not box.offer(job): if box.closed: raise Rejected("the pool is closed") box.drop_oldest()class WaitAMoment: def __init__(self, seconds): self.seconds = seconds def handle(self, box, job): if not box.offer(job, self.seconds): raise Rejected(f"no place after {self.seconds} s")Lines 7 to 9
The honest default. Fast, and the caller can tell its user the truth.
1 of 4
Walk
A slow dependency, minute by minute
40 workers, normally 0.2 s per task, 150 tasks per second
| Time | What happens | Without a limit | With 100 places and a refusal |
|---|---|---|---|
| 0 s | Busy: 150 x 0.2 = 30 of 40 | Fine | Fine |
| 10 s | The payment service now needs 2 s | 40 / 2 = 20 tasks per second are done | the same |
| 11 s | 130 too many per second | 130 waiting | 100 waiting, the rest hears busy |
| 70 s | One minute of it | 7,800 waiting, 390 s of work | 100 waiting, 5 s of work |
| 71 s | The service is fast again: 200 per second | Needs 7,800 / (200 - 150) = 156 s to catch up | Needs 100 / 50 = 2 s |
Requests time out after 3 seconds at the caller. Your queue holds 5 seconds of work. What is wrong, and what do you do?
BreaksNothing, the queue protects us
- How
- Leave it.
- Limit
- At full load every task is done for nobody: 100 percent busy, 0 percent useful.
- Choose when
- Never.
WorksShorten the queue
- How
- Below 3 seconds of work.
- Limit
- The speed changes with the dependency, so the number is never right for long.
- Choose when
- As a first step.
StrongDeadline with the task
- How
- Each task carries its deadline. A worker that takes an expired one drops it at once, and submit refuses if it cannot make it.
- Limit
- The deadline must travel with the request.
- Choose when
- Any chain of services with timeouts.