Lesson 2 of 4
Stealing from the Busiest List
Estimated time: 10 minutes
Stealing from the busiest list
Most designs keep a small local list per worker so they do not all contend on one shared queue. That reintroduces the original problem in miniature, because an item already on a local list is claimed and a free worker elsewhere cannot reach it.
Work stealing is the escape hatch. An idle worker may take an item from the tail of a busy worker's local list, so a long item on one list does not leave another worker waiting while the queue is empty.
Keep the local lists short. Large lists defeat both halves of the pattern: they reduce the chance the shared queue has anything left to take, and they leave less on the tail for a stealing worker, so the pool drifts back towards fixed assignment.
STEAL_LIMIT = 3
def steal(victim, limit=STEAL_LIMIT):
if len(victim) <= limit:
return None
return victim.pop()
The limit stops a pool from shuffling a short list back and forth instead of doing work.