Is there a way to define a RabbitMQ consumer that keeps listening for a queue without blocking the program?

43 Views Asked by At

I am working on project, using RabbitMq in python. Initially an Admin Verify an order which is then published in a RabbitMq message queue 'verified_orders', I want to have a consumer that keeps listening for this MQ , consume an order and send it back with details to the client, but without blocking the state of the whole program.

I have seen RabbitMQ tutorials in which I found this code Suppose that the connection to the RabbitMQ is already established

Code to publish messages

channel.basic_publish(exchange='',
                      routing_key='verified_orders',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")

Code to Consume

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

channel.basic_consume(queue='verified_orders',
                      auto_ack=True,
                      on_message_callback=callback)

channel.start_consuming()

When executing start_consuming() it will infinitely waits for data and runs callbacks, however the program will enter a blocking state which I am trying to avoid.

Another approach that I found is to define an async function with an infinite loop to consume messages, but I don't believe that it is a good approach to use infinite loop.

channel.basic_get(queue='verified_orders', auto_ack=True)
1

There are 1 best solutions below

0
Hamed Ghennat On

If you want a non-blocking program, you must use concurrency. There are two ways to implement it, one is using multi-threading and the other is asynchronous programming.

The Async method is much more optimal because you can achieve your goal by using an infinite loop inside a single thread. Also you use async when you have an IO bound task and connecting, consuming from or publishing to a MQ is an IO bound task.

As I understood from your code sample, you are using the pika library. This library is implemented as sync. You can use aiopika instead, which provides all the features of Rabbitmq as async.