How to implement priority queue using RabbitMQ using Python client(Pika/Kombu) or any other Task Queue ?
P.S. -> To implement Task Queue in Message Prioritization.
You can declare a priority queue using pika
.
channel.queue_declare('queue_name', {''x-max-priority': 10})
To send a message with priority
channel.basic_publish(exchange='exchange_name',
routing_key='routing_key',
body='Messsage',
properties=BasicProperties(priority=1))
You can set the priority
value between 1 to 10, i.e. the maximum priority value that you have set. You can set a maximum priority value of 255, but RabbitMQ recommends the maximum value to be 10. Reference
To consume the message, you define a callback function and call the basic_consume
method
def callback(ch, method, properties, body):
# Getting the message
message = body.decode()
## Do the logic
print('Received message {}'.format(message))
# Sending acknowledgment back
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume(callback, queue='queue_name')
The consumer for a priority queue is the same as the consumer for a normal queue. When you set a priority the messages get reordered based on the priority at the broker side. So you consume the messages normally without getting bothered about the messages.
Note: You cannot redeclare a queue as a priority queue. You gotta delete the old queue and declare the new one as a priority queue.