Lesson 1 of 4
One Queue, and Workers That Pull
Estimated time: 10 minutes
One queue, and workers that pull
Assigning work to a worker in advance is simple and it fails on uneven items. One worker draws the long ones while another finishes its short list and waits, so the pool is busy and idle at once and the slowest item decides when the batch ends.
Pull based dispatch replaces the assignment with one shared queue that each worker takes from when it becomes free. Idle time falls to the gap between finishing one item and taking the next, and an uneven item no longer pins work to a worker that cannot reach it.
The reading that shows whether this is working is per worker depth beside the shared queue depth. One worker idle while the shared queue is not empty is the exact signature of work held away from a free worker.
def take_work(shared, others):
if shared:
return shared.pop()
for other in sorted(others, key=len, reverse=True):
if other:
return other.pop()
return None
A single pool-wide number hides that imbalance, which is why the depth is read per worker.