0
0
RabbitMQdevops~30 mins

Work queue for task distribution in RabbitMQ - Mini Project: Build & Apply

Choose your learning style9 modes available
Work queue for task distribution
📖 Scenario: You are building a simple task distribution system using RabbitMQ. Multiple workers will receive tasks from a queue to share the workload evenly.
🎯 Goal: Create a RabbitMQ work queue named task_queue. Write a producer script that sends 5 tasks with specific messages to the queue. Then write a consumer script that receives and prints each task message.
📋 What You'll Learn
Create a durable queue named task_queue
Producer sends 5 messages: Task 1, Task 2, Task 3, Task 4, Task 5
Consumer receives messages from task_queue and prints them
Use manual message acknowledgments in the consumer
💡 Why This Matters
🌍 Real World
Work queues are used in real systems to distribute tasks like image processing, email sending, or data analysis among multiple worker machines.
💼 Career
Understanding work queues and message brokers like RabbitMQ is essential for DevOps engineers and backend developers to build scalable and reliable distributed systems.
Progress0 / 4 steps
1
Create a durable queue named task_queue
Write a Python script that connects to RabbitMQ on localhost and declares a durable queue named task_queue. Use the pika library and set durable=True when declaring the queue.
RabbitMQ
Need a hint?

Use pika.BlockingConnection to connect and channel.queue_declare with durable=True.

2
Send 5 tasks to the task_queue
Add code to the script to send 5 messages: 'Task 1', 'Task 2', 'Task 3', 'Task 4', and 'Task 5' to the task_queue. Make sure to mark messages as persistent by setting delivery_mode=2 in BasicProperties.
RabbitMQ
Need a hint?

Use a for loop from 1 to 5 and channel.basic_publish with delivery_mode=2 for persistence.

3
Create a consumer to receive and print tasks
Write a Python script that connects to RabbitMQ, declares the same durable queue task_queue, and consumes messages. Use channel.basic_consume with a callback function that prints the received message body. Use manual acknowledgments by calling ch.basic_ack inside the callback.
RabbitMQ
Need a hint?

Define a callback function that prints the message and calls basic_ack. Use channel.basic_consume with this callback.

4
Run the consumer and verify output
Run the consumer script to receive and print all 5 task messages sent earlier. The output should show:
Received Task 1
Received Task 2
Received Task 3
Received Task 4
Received Task 5
RabbitMQ
Need a hint?

Run the consumer script after the producer has sent messages. The output should print each task received.