BLPOP removes and returns the leftmost value from a Redis list. When the list is empty, it waits for an item instead of returning immediately, allowing worker processes to sleep without busy-waiting. section 10.1.1
Use BLPOP in a Redis-backed FIFO task queue:
- Add tasks to the back with RPUSH. section 10.1.1
- Have workers call BLPOP on the queue’s name. section 10.1.1
- Let Redis wait for work rather than implementing a retry loop that consumes resources. section 10.1.1
Worker task retrieval
A Python worker repeatedly executes r.blpop('queue:task'), reads the returned task value, calculates Pi, and then waits for the next task. section 10.1.1
Listing 10.2 Chapter10/pi_worker/pi_worker.py
import os
import redis
from pi import *
redis_host = os.environ.get('REDIS_HOST') #1
assert redis_host != None
r = redis.Redis(host=redis_host, #2
port='6379', #2
decode_responses=True) #2
print("starting")
while True:
task = r.blpop('queue:task') #3
iterations = int(task[1])
print("got task: " + str(iterations))
pi = leibniz_pi(iterations) #4
print (pi)
Shows BLPOP inside a complete Redis worker loop, including queue selection and task-value handling. section 10.1.1
