liveBook · Manning

BLPOP command

As explained in Kubernetes for Developers

The Redis BLPOP command removes and returns the leftmost value from a list, blocking until a value is available when the list is empty.

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

How Kubernetes for Developers covers it

How deep it goes
Explains FIFO placement, blocking behavior, busy-wait avoidance, and a Python worker loop, but not command options.
Examples
Demonstrates Python workers retrieving Pi-calculation tasks from queue:task.
What it assumes
Assumes familiarity with Redis lists and basic worker processes while establishing queue ordering from the operation names.
What you can do after
Apply it via Redis and a worker loop to consume background tasks without busy-waiting.
How it connects
Connects Redis lists with RPUSH, LPOP, FIFO queues, worker processes, and Kubernetes Deployments.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.