如何在 RabbitMQ/pika 中实现优先级队列

发布于 2024-12-09 08:45:38 字数 222 浏览 0 评论 0原文

我希望用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微暖i 2024-12-16 08:45:38

接受的答案已过时。
rabbitmq 3.5.0 开始,优先级队列有了原生支持

RabbitMQ有优先级队列
从版本 3.5.0 开始在核心中实现。任何队列都可以
使用客户端提供的可选参数变成优先级

它也是 pika 1.1.0 起可用

类 pika.spec.BasicProperties(content_type=None,
content_encoding=无,标头=无,delivery_mode=无,
优先级=无,correlation_id=无,reply_to=无,过期=无,
message_id=无、时间戳=无、类型=无、user_id=无、app_id=无、
cluster_id=无)

使用此功能的代码可能如下所示:

channel.basic_publish(properties=pika.BasicProperties(priority=your_priority),
                      exchange=...,
                      routing_key=...,
                      body=...)

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=...)
缱绻入梦 2024-12-16 08:45:38

我认为没有办法在鼠兔的消费者层面天真地做到这一点,因为默认情况下所有消费者都具有相同的优先级。

为了解决这个问题,我可能要做的就是按照邮件列表上的建议设置两个队列,每个队列都有自己的消费者。在每个消费者的消费者回调中,我不会直接处理消息,而是将其放入优先级队列中,然后调用一个从队列中读取最高优先级消息并处理它的函数。

另一个问题也有类似的回答。

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.

绝不放开 2024-12-16 08:45:38

如果您在接受后遇到这个问题。
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文