Get Queue Size in Pika (AMQP Python)

后端 未结 4 1551
無奈伤痛
無奈伤痛 2020-12-09 02:00

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?

4条回答
  •  囚心锁ツ
    2020-12-09 02:50

    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.

提交回复
热议问题