Applying the leader/followers pattern
When you have a stream of tasks the leader/followers pattern can be useful as a way of encapsulating task generation/execution responsibilities into different components. A common scenario in which this pattern is applied involves multithreaded web servers in which the leader process sets up a pool of worker threads waiting to handle concurrent requests. As we will explore in the next chapter, this model can be scaled out using entirely asynchronous implementation of the web servers.
The idea is that a leader takes incoming task requests and assigns the actual operation to workers from a pool, as shown in the following example (available at Chapter 6/leader-follower.py) of a simple single-node implementation of the pattern:
import asyncio
import random
async def worker(queue, worker_id):
while True:
task = await queue.get()
if task is None:
queue.task_done()
break
print(f"Worker...