Introduction
In this article, we will guide you through setting up and using RabbitMQ with Python. RabbitMQ is a powerful message broker that allows applications to communicate with each other via messages. This practical guide will show you how to connect to RabbitMQ, publish messages to a queue, and consume messages from a queue using Python. Additionally, we will use Docker to manage RabbitMQ in a containerized environment, ensuring a smooth and isolated setup. Whether you are new to message brokers or looking to integrate RabbitMQ into your Python projects, this guide will provide you with a solid foundation to get started.
Summary
In this article, we will cover the essentials of setting up and using RabbitMQ with Python. You will learn how to:
- Set up your development environment.
- Use Docker to run RabbitMQ in a container.
- Connect to RabbitMQ from a Python application.
- Publish messages to a queue.
- Consume messages from a queue.
- Access the RabbitMQ management console.
By the end of this guide, you will have a working example of a RabbitMQ setup with Python and Docker, and you will be ready to integrate these tools into your own projects.
Prerequisites
Before we begin, make sure you have the following tools and technologies installed on your system:
- Python 3.8+: Ensure you have Python installed. You can download it from the official Python website.
- Docker: Docker is required to run RabbitMQ in a containerized environment. You can download and install Docker from the official Docker website.
- Docker Compose: Docker Compose is used to manage multi-container Docker applications. It is included with Docker Desktop.
Having these prerequisites installed will ensure a smooth setup and execution of the examples provided in this guide.
Environment Setup
To get started, follow these steps to set up your development environment:
- Create and activate the virtual environment
Creating a virtual environment helps to isolate your project’s dependencies. Here’s how you can create and activate a virtual environment:
-
Create the virtual environment:
python -m venv .venv
-
Activate the virtual environment:
- On Windows:
.venv\Scripts\activate
- On macOS/Linux:
source .venv/bin/activate
- Install the dependencies
Next, install the required Python packages. For this project, we will use the pika library to interact with RabbitMQ.
-
Install the required packages:
pip install pika
-
Save the installed packages to requirements.txt:
pip freeze > requirements.txt
With the environment set up and dependencies installed, you’re ready to move on to the next steps in configuring RabbitMQ with Docker.
Docker Setup
In this section, we will configure and start RabbitMQ using Docker. This allows us to easily manage and isolate RabbitMQ in a containerized environment.
- Create a
docker-compose.yaml
file
Create a file named docker-compose.yaml
in the root of your project directory. Add the following content to the file:
version: '3.8'
services:
rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
ports:
- '5672:5672'
- '15672:15672'
environment:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
This configuration sets up RabbitMQ with the management plugin enabled, allowing you to access the RabbitMQ management console on port 15672.
- Start the RabbitMQ container
Run the following command to build and start the RabbitMQ container:
docker-compose up -d
This command will download the RabbitMQ Docker image (if not already available locally), start the container, and run it in the background. The RabbitMQ server will be accessible at localhost:5672
, and the management console will be available at http://localhost:15672
.
With RabbitMQ running in a Docker container, you are ready to move on to the next steps of developing the application and integrating RabbitMQ with your Python code.
Application Development
In this section, we will develop the application that connects to RabbitMQ, publishes messages to a queue, and consumes messages from a queue. We’ll start by implementing a class to manage the connection and interactions with RabbitMQ.
- Implement the RabbitMQ class
Create a file named rabbitmq.py
in the root of your project directory. Add the following content to the file:
import pika
import os
class RabbitMQ:
def __init__(self):
self.user = os.getenv('RABBITMQ_USER', 'user')
self.password = os.getenv('RABBITMQ_PASSWORD', 'password')
self.host = os.getenv('RABBITMQ_HOST', 'localhost')
self.port = int(os.getenv('RABBITMQ_PORT', 5672))
self.connection = None
self.channel = None
self.connect()
def connect(self):
credentials = pika.PlainCredentials(self.user, self.password)
parameters = pika.ConnectionParameters(host=self.host, port=self.port, credentials=credentials)
self.connection = pika.BlockingConnection(parameters)
self.channel = self.connection.channel()
def close(self):
if self.connection and not self.connection.is_closed:
self.connection.close()
def consume(self, queue_name, callback):
if not self.channel:
raise Exception("Connection is not established.")
self.channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
self.channel.start_consuming()
def publish(self, queue_name, message):
if not self.channel:
raise Exception("Connection is not established.")
self.channel.queue_declare(queue=queue_name, durable=True)
self.channel.basic_publish(exchange='',
routing_key=queue_name,
body=message,
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
))
print(f"Sent message to queue {queue_name}: {message}")
This class handles connecting to RabbitMQ, publishing messages to a queue, and consuming messages from a queue. The connection parameters are read from environment variables.
- Create the main.py script
Create a file named main.py
in the root of your project directory. Add the following content to the file:
from rabbitmq import RabbitMQ
import sys
def callback(ch, method, properties, body):
print(f"Received message: {body}")
def main():
rabbitmq = RabbitMQ()
try:
print("Connection to RabbitMQ established successfully.")
rabbitmq.consume(queue_name='test_queue', callback=callback)
except Exception as e:
print(f"Failed to establish connection to RabbitMQ: {e}")
sys.exit(1)
finally:
rabbitmq.close()
if __name__ == "__main__":
main()
This script connects to RabbitMQ and starts consuming messages from a queue named test_queue.
- Create the publisher.py script
Create a file named publisher.py
in the root of your project directory. Add the following content to the file:
from rabbitmq import RabbitMQ
def publish_test_message():
rabbitmq = RabbitMQ()
try:
rabbitmq.publish(queue_name='test_queue', message='Test message')
print("Test message published successfully.")
except Exception as e:
print(f"Failed to publish test message: {e}")
finally:
rabbitmq.close()
if __name__ == "__main__":
publish_test_message()
This script publishes a test message to the test_queue.
With these scripts in place, you are ready to run and test your application.
Running the Application
In this section, we will run the application to ensure everything is set up correctly and that we can successfully publish and consume messages using RabbitMQ.
- Start the RabbitMQ server
Run the main.py
script to start the RabbitMQ server and begin consuming messages from the test_queue:
python main.py
You should see a message indicating that the connection to RabbitMQ was established successfully. The script will continue running and wait for messages to consume from the test_queue.
- Publish a test message
Open a new terminal window or tab, and run the publisher.py script to publish a test message to the test_queue:
python publisher.py
You should see a message indicating that the test message was published successfully. The main.py script should also display the received message, indicating that the message was successfully consumed from the test_queue.
Example Output
When you run main.py, you should see something like this:
Connection to RabbitMQ established successfully.
Received message: b'Test message'
When you run publisher.py, you should see something like this:
Sent message to queue test_queue: Test message
Test message published successfully.
Conclusion
In this guide, we covered the steps to set up and use RabbitMQ with Python. We demonstrated how to configure the development environment, run RabbitMQ in a Docker container, and create a simple application to publish and consume messages. This should give you a solid foundation to start integrating RabbitMQ into your own projects.
For more information and advanced usage of RabbitMQ, please refer to the official RabbitMQ documentation.
GitHub Repository
You can find the complete code for this project in the following GitHub repository: python-rabbitmq.
This repository contains all the scripts and configurations discussed in this article, including the docker-compose.yaml, rabbitmq.py, main.py, publisher.py, and the README.md with detailed setup instructions. Feel free to clone the repository and experiment with the code to further your understanding of RabbitMQ and Python integration.
Top comments (1)
Hey Felipe, great intro guide to getting familiar working with RabbitMQ! One thing I noticed is that if you have to run the
Producer.py
first, if you haven't created thetest_queue
yet. If you runMain.py
before creating thetest_queue
, you will get a message saying(404, "NOT_FOUND - no queue 'test_queue' in vhost '/'")
.