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

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

问题描述

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

解决方案

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

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

关于Pika,我不知道具体细节,但是AMQP的Python客户端在我这边很棘手.通常,您将需要使用猴子补丁类来获取所需的信息,或者允许队列使用方超时,以便您可以定期执行其他操作,例如记录统计信息或找出队列中有多少消息.

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

我祈祷其他拥有更多Pika经验的人通过指出如何完成我提到的所有事情来回答这个问题,在这种情况下,我将下载Pika并努力尝试.但是,如果没有发生这种情况,而您又难以在猴子补丁程序中修补Pika代码,那么请看一下haigha.我发现它们的代码比其他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天全站免登陆