在 Pika 中获取队列大小(AMQP Python) [英] Get Queue Size in Pika (AMQP Python)

查看:26
本文介绍了在 Pika 中获取队列大小(AMQP Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,但 Google 或 Pika 开源代码没有帮助.有没有办法在 Pika 中查询当前队列大小(项目计数器)?

解决方案

在AMQP协议中有两种获取队列大小的方法.您可以使用 Queue.Declare 或 Basic.Get.

如果您在消息到达时使用 Basic.Consume 进行消费,那么除非您断开连接(超时)并重新声明队列,否则您将无法获得此信息,或者收到一条消息但不确认.在较新版本的 AMQP 中,您可以主动重新排队消息.

至于 Pika,我不知道具体细节,但 AMQP 的 Python 客户端一直是我的眼中钉.通常,您需要使用monkeypatch 类来获取您需要的信息,或者允许队列使用者超时,以便您可以定期执行其他操作,例如记录统计信息或找出队列中有多少消息.

另一种解决方法是放弃,使用 Pipe 类运行 sudo rabbitmqctl list_queues -p my_vhost.然后解析输出以找到所有队列的大小.如果你这样做,你需要配置 /etc/sudoers 不要求通常的 sudo 密码.

我祈祷有更多 Pika 经验的其他人通过指出您如何完成我提到的所有事情来回答这个问题,在这种情况下,我将下载 Pika 并踢轮胎.但是,如果这没有发生,并且您在对 Pika 代码进行猴子修补时遇到困难,那么请查看 haiigha.我发现他们的代码比其他 Python AMQP 客户端库简单得多,因为它们更接近 AMQP 协议.

Simple question, but Google or the Pika open source code did not help. Is there a way to query the current queue size (item counter) in Pika?

解决方案

There are two ways to get the queue size in the AMQP protocol. You can either use Queue.Declare or Basic.Get.

If you are consuming messages as they arrive using Basic.Consume, then you can't get this info unless you disconnect (timeout) and redeclare the queue, or else get one message but don't ack it. In newer versions of AMQP you can actively requeue the message.

As for Pika, I don't know the specifics but Python clients for AMQP have been a thorn in my side. Often you will need to monkeypatch classes in order to get the info you need, or to allow a queue consumer to timeout so that you can do other things at periodic intervals like record stats or find out how many messages are in a queue.

Another way around this is to give up, and use the Pipe class to run sudo rabbitmqctl list_queues -p my_vhost. Then parse the output to find the size of all queues. If you do this you will need to configure /etc/sudoers to not ask for the usual sudo password.

I pray that someone else with more Pika experience answers this by pointing out how you can do all the things that I mentioned, in which case I will download Pika and kick the tires. But if that doesn't happen and you are having difficulty with monkeypatching the Pika code, then have a look at haigha. I found their code to be much more straightforward than other Python AMQP client libraries because they stick closer to the AMQP protocol.

这篇关于在 Pika 中获取队列大小(AMQP Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆