Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement Priority Queues in RabbitMQ/pika

I am looking to implement a priority queue with RabbitMQ. The mailing list recommends to use multiple queues, each queue representing a different priority level.

My question is, how do you poll multiple queues in some prioritized order using pika (or possibly some other python library)?

like image 432
acrophobia Avatar asked Oct 12 '11 15:10

acrophobia


1 Answers

The accepted answer is outdated. From rabbitmq 3.5.0 there's native support for priority queues:

RabbitMQ has priority queue implementation in the core as of version 3.5.0. Any queue can be turned into a priority one using client-provided optional arguments

It's also available as of pika 1.1.0

class pika.spec.BasicProperties(content_type=None, content_encoding=None, headers=None, delivery_mode=None, priority=None, correlation_id=None, reply_to=None, expiration=None, message_id=None, timestamp=None, type=None, user_id=None, app_id=None, cluster_id=None)

The code using this feature might look as follows:

channel.basic_publish(properties=pika.BasicProperties(priority=your_priority),
                      exchange=...,
                      routing_key=...,
                      body=...)
like image 76
ffeast Avatar answered Sep 22 '22 11:09

ffeast