RabbitMQ — Producer in Python

Himanshu Sharma
3 min readMay 1, 2021

In the previous blog, we discussed what is RabbitMQ, its Architecture, and the Environment & installation setup process on an Ubuntu system.
In this blog post, we’ll discuss the implementation of the RabbiMQ Producer via Kombu Python.

This architecture revolves around three essential components, producer, exchange, and queues.

Producer:
A producer is a user application that sends messages to the Queue. A Producer act as an entry point to the RabbiMQ Architecture.

Producer Workflow

Note: The producer does not have the capabilities to communicate any messages directly to the queue. Rather, the producer can only send these messages to the exchange and in turn the Exchange act as a receiver pushing these data streams into the queue.

Producer sends data via Exchange

Exchange: Exchanges are message routing agents, defined by the virtual host within RabbitMQ. Exchange is helpful for routing the messages from producers to queues using bindings, routing keys, and headers.

Queue: Queue will hold the data received from the exchange till ingested by a consumer to perform the queued-up asynchronous task. Once these tasks are ingested, new tasks are queued up into a ready state.

Let’s go ahead and build the Producer :

We need Python3 and Kombu library(it uses AMQP protocol).

Let’s install kombu for the producer first; Some alternatives to kombu are pika and aioampq.

pip3 install kombu

Now will create a producer script as producer.py. Let’s divide the code into separate functionalities as :

Package Imports :
Firstly, we need to import the package and submodules that we are going to use as

from kombu import Connection, Producer, Exchange

Connection Config:
Next, let's define the connection settings in the config.py file that will help us get the settings set in the RabbitMQ server.

config = {
'rabbit_url' : 'amqp://guest:guest@localhost//',
'exchange' : 'test',
'exchange_type' : 'direct',
'routing_key' : 'test',
}

Specifying the Exchange: We can create the queue and make the binding with the same in order to send the data. As an alternative, we can send the data directly via Exchange but, we would be required to define the exchange and its type in the Exchange function.

from config import config
exchange = Exchange(config.get('exchange'),"direct", durable=True)

Creating a Connection Object: The Connection Object requires two inputs, the Host URL and the virtual host to be defined and passed to the final Connection function. Later this object can be further passed on to the producer function for linking up a channel for communication with the queues.

connection = Connection('amqp://guest:guest@localhost//', virtual_host=’/’)

Creating and Publishing the Producer: We need to create a producer object that will send the payload and all we need to pass is a connection, exchange, routing_key, and the payload values in the Producer function.

producer = Producer(connection, exchange = "test",  routing_key = "test")
producer.declare()
producer.publish("First Message")

Voila!!!, we got the data in the RabbitMQ Queue.

RabbitMQ Queue Data published by The Producer

With this, we come to the end of this blog, which hopefully has helped you to setup your own RabbitMQ producer function.

The Full codebase is available in my git repo, please do let me know if you have any doubts with the RabbitMQ setup or code blocks.

Thanks for reading!!!!!

--

--

Himanshu Sharma

Full Stack Developer | Cyber Security Analyst | SDE-2 @Angel One