如何在 RabbitMQ/pika 中实现优先级队列
我希望用 RabbitMQ 实现优先级队列。邮件列表建议使用多个队列,每个队列代表不同的优先级。
我的问题是,如何使用 pika (或者可能是其他一些 python 库)按优先顺序轮询多个队列?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
接受的答案已过时。
从
rabbitmq 3.5.0
开始,优先级队列有了原生支持:它也是 自
pika 1.1.0
起可用使用此功能的代码可能如下所示:
The accepted answer is outdated.
From
rabbitmq 3.5.0
there's native support for priority queues:It's also available as of
pika 1.1.0
The code using this feature might look as follows:
我认为没有办法在鼠兔的消费者层面天真地做到这一点,因为默认情况下所有消费者都具有相同的优先级。
为了解决这个问题,我可能要做的就是按照邮件列表上的建议设置两个队列,每个队列都有自己的消费者。在每个消费者的消费者回调中,我不会直接处理消息,而是将其放入优先级队列中,然后调用一个从队列中读取最高优先级消息并处理它的函数。
另一个问题也有类似的回答。
I don't think there is a way to do it naively at the consumer level with pika, as all consumers by default have the same priority.
What I might do to solve the problem would be to have the two queues as suggested on the mailing list, each queue with its own consumer. In the consumer callback for each consumer instead of dealing with the message directly I would put it into a priority queue, then call a function that reads the highest priority message from the queue and handles it.
Another question with a similar response.
如果您在接受后遇到这个问题。
RabbitMQ 有一个插件,允许设置一个具有优先级的队列:
https://github.com/rabbitmq/rabbitmq-priority-queue
In case you stumble into this question after it's been accepted.
RabbitMQ has a plugin that allows to set up one queue with priorites:
https://github.com/rabbitmq/rabbitmq-priority-queue