Using the Docker image
|
|
Introduction
Hello World!
Python
|
|
Sending
|
|
establish a connection with RabbitMQ server:
|
|
Receiving
|
|
|
|
Work Queues
The main idea behind Work Queues (aka: Task Queues) is to avoid doing a resource-intensive task immediately and having to wait for it to complete. Instead we schedule the task to be done later. We encapsulate a task as a message and send it to the queue. A worker process running in the background will pop the tasks and eventually execute the job. When you run many workers the tasks will be shared between them.
Round-robin dispatching
One of the advantages of using a Task Queue is the ability to easily parallelise work. If we are building up a backlog of work, we can just add more workers and that way, scale easily.
First, let’s try to run two worker.py scripts at the same time. You need three consoles open. Two will run the worker.py script. These consoles will be our two consumers - C1 and C2. In the third one we’ll publish new tasks.
|
|
|
|
|
|
By default, RabbitMQ will send each message to the next consumer, in sequence. On average every consumer will get the same number of messages. This way of distributing messages is called round-robin.
Message acknowledgment
With our current code once RabbitMQ delivers message to the consumer, it immediately marks it for deletion. In this case, if you terminate a worker, the message it was just processing is lost. The messages that were dispatched to this particular worker but were not yet handled are also lost.
But we don’t want to lose any tasks. If a worker dies, we’d like the task to be delivered to another worker.
In order to make sure a message is never lost, RabbitMQ supports message acknowledgments. An ack(nowledgement) is sent back by the consumer to tell RabbitMQ that a particular message had been received, processed and that RabbitMQ is free to delete it.
If a consumer dies (its channel is closed, connection is closed, or TCP connection is lost) without sending an ack, RabbitMQ will understand that a message wasn’t processed fully and will re-queue it. If there are other consumers online at the same time, it will then quickly redeliver it to another consumer. That way you can be sure that no message is lost, even if the workers occasionally die.
Manual message acknowledgments are turned on by default. We explicitly turned them off via the auto_ack=True flag.
|
|
Using this code, you can ensure that even if you terminate a worker using CTRL+C while it was processing a message, nothing is lost. Soon after the worker terminates, all unacknowledged messages are redelivered.
Message durability
Two things are required to make sure that messages aren’t lost if RabbitMQ server stops: we need to mark both the queue and messages as durable.
|
|
At that point we’re sure that the task_queue queue won’t be lost even if RabbitMQ restarts.
Now we need to mark our messages as persistent - by supplying a delivery_mode property with the value of pika.DeliveryMode.Persistent
|
|
Marking messages as persistent doesn’t fully guarantee that a message won’t be lost. Although it tells RabbitMQ to save the message to disk, there is still a short time window when RabbitMQ has accepted a message and hasn’t saved it yet. If you need a stronger guarantee then you can use publisher confirms.
Fair dispatch
We can use the Channel#basic_qos channel method with the prefetch_count=1 setting, don’t dispatch a new message to a worker until it has processed and acknowledged the previous one. Instead, it will dispatch it to the next worker that is not still busy.
Python
|
|
Task
|
|
|
|
Worker
Use time.sleep() to pretend to be a time-consuming work task.
|
|
|
|