RabbitMQ Tutotial for Python (using Pika)

What is RabbitMQ?

RabbitMQ is an open-source message broker software that originally implemented the Advanced Message Queuing Protocol (AMQP). It facilitates the efficient delivery of messages in a distributed system, enabling applications to communicate with each other through message passing.

RabbitMQ is used for its reliability, scalability, and flexibility. It's particularly useful in decoupling systems or components, allowing for asynchronous processing, which can improve the scalability and efficiency of applications.

Use Cases:

  • Asynchronous Task Processing: Offloading tasks that can be performed later, like sending emails or processing images.
  • Inter-Service Communication: Enabling microservices to communicate in a decoupled manner.
  • Distributed Computing: Coordinating and distributing workloads across various computing nodes.


We'll show how to create a producer (a.k.a. sender) for sending messages to a RabbitMQ queue as well as a consumer (a.k.a. listener or receiver) for receiving messages from the queue.


  • Python environment
  • RabbitMQ server (Install guide: RabbitMQ Installation)
  • pika library (Install using pip install pika)

Creating a Producer (a.k.a. Sender)

Step 1: Import Pika Library

import pika

Step 2: Establish Connection

Connect to a RabbitMQ server:

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

Step 3: Declare a Queue

Declare a queue to send messages:


Step 4: Send a Message

Publish a message to the queue:

                      body='Hello World!')
print(" [x] Sent 'Hello World!'")

Creating a Consumer (a.k.a. Listener/Receiver)

Step 1: Import Pika and Set Up Connection

Similar to the producer, import pika and set up the connection:

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

Step 2: Declare the Same Queue

Make sure to declare the same queue as the producer:


Step 3: Define a Callback Function

Define the function to be called when a message is received:

def callback(ch, method, properties, body):
    print(f" [x] Received {body}")

Step 4: Set Up Queue Consumption

Tell RabbitMQ that this callback function should receive messages from the queue:


print(' [*] Waiting for messages. To exit press CTRL+C')


With RabbitMQ and Python, you can set up robust and scalable message-based communication for your applications. This guide provides the basic steps to create a producer and consumer in Python, enabling you to implement efficient message handling in your system.