Message Queues
A message queue is a waiting line where a producer leaves jobs that a worker will do later. This separation is called decoupling, and it lets each side run at its own speed.
A producer adds five jobs
The producer adds each job to the end of a list. The length printout grows by one after every addition.
queue = []
jobs = ["job-1", "job-2", "job-3", "job-4", "job-5"]
for job in jobs:
queue.append(job)
print("Added", job, "- queue length:", len(queue))A worker takes the jobs in order
The worker removes the job at the front of the list each time. The processing order matches the order the jobs were added.
queue = ["job-1", "job-2", "job-3", "job-4", "job-5"]
while queue:
job = queue.pop(0)
print("Processed", job, "- queue length:", len(queue))A slow worker lets the queue grow
Each round adds two jobs and the worker finishes only one. The length grows after every round.
queue = []
next_job = 1
for round_number in range(1, 4):
queue.append("job-" + str(next_job))
next_job = next_job + 1
queue.append("job-" + str(next_job))
next_job = next_job + 1
queue.pop(0)
print("After round", round_number, "- queue length:", len(queue))Example
queue = []
for number in range(1, 6):
job = "job-" + str(number)
queue.append(job)
print("Added", job, "- queue length:", len(queue))
while queue:
job = queue.pop(0)
print("Processed", job, "- queue length:", len(queue))Key points
- Message queue: a list that holds jobs in order until a worker takes them.
- Producer: the program that adds jobs to the queue.
- Worker: a program that takes jobs from the queue and does the work in them.
- First in, first out: the first job added is the first job taken.
In this example, the producer adds all five jobs before the worker takes the first one, and the worker still receives them in the order they were added. That separation is decoupling at work, and the lengths show the queue holding the gap between the two speeds.
Note
a queue that keeps growing means jobs arrive faster than the worker finishes them.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Jobs arrive faster than the worker can finish them for a long stretch. What happens to the queue?