Python 获取RabbitMQ队列中的消息数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16691161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:22:58  来源:igfitidea点击:

Getting number of messages in a RabbitMQ queue

pythonrabbitmqmessage-queuepy-amqplib

提问by Basic

We're using amqplibto publish/consume messages. I want to be able to read the number of messages on a queue (ideally both acknowledged and unacknowledged). This will allow me to show a nice status diagram to the admin users and detect if a certain component is not keeping up with the load.

我们使用amqplib来发布/使用消息。我希望能够读取队列中的消息数量(理想情况下既已确认又未确认)。这将允许我向管理员用户显示一个很好的状态图,并检测某个组件是否跟不上负载。

I can't find any information in the amqplib docs about reading queue status.

我在 amqplib 文档中找不到关于读取队列状态的任何信息。

Can someone point me in the right direction?

有人可以指出我正确的方向吗?

采纳答案by ChillarAnand

Using pika:

使用鼠兔:

import pika

pika_conn_params = pika.ConnectionParameters(
    host='localhost', port=5672,
    credentials=pika.credentials.PlainCredentials('guest', 'guest'),
)
connection = pika.BlockingConnection(pika_conn_params)
channel = connection.channel()
queue = channel.queue_declare(
    queue="your_queue", durable=True,
    exclusive=False, auto_delete=False
)

print(queue.method.message_count)

Using PyRabbit:

使用 PyRabbit:

from pyrabbit.api import Client
cl = Client('localhost:55672', 'guest', 'guest')
cl.get_messages('example_vhost', 'example_queue')[0]['message_count']

Using HTTP

使用 HTTP

Syntax:

句法:

curl -i -u user:password http://localhost:15672/api/queues/vhost/queue

Example:

例子:

curl -i -u guest:guest http://localhost:15672/api/queues/%2f/celery           

Note: Default vhost is /which needs to be escaped as %2f

注意:默认 vhost/需要转义为%2f

Using CLI:

使用命令行:

$ sudo rabbitmqctl list_queues | grep 'my_queue'

回答by mguillermin

Using the Java API, you can do the following :

使用 Java API,您可以执行以下操作:

channel.queueDeclarePassive(queueName).getMessageCount()

I believe this is also available with amqplib (according to https://code.google.com/p/py-amqplib/source/browse/amqplib/client_0_8/channel.py#1356it seems that queue_declare()returns a tuple with the message count)

我相信这也适用于 amqplib(根据https://code.google.com/p/py-amqplib/source/browse/amqplib/client_0_8/channel.py#1356似乎queue_declare()返回一个带有消息计数的元组)

If you need more precise metrics (especially nack message count), you need to use rabbitmqctl or rabbitmq_management. Rabbitmq_management is probably a good choice due to its HTTP API. More info : http://www.rabbitmq.com/management.html

如果您需要更精确的指标(尤其是 nack 消息计数),则需要使用 rabbitmqctl 或 rabbitmq_management。由于其 HTTP API,Rabbitmq_management 可能是一个不错的选择。更多信息:http: //www.rabbitmq.com/management.html

回答by OhadBasan

following the answer of ChillarAnand you can get the value easily. the data is in the object.

按照 ChillarAnand 的回答,您可以轻松获得价值。数据在对象中。

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost',
            port=5672,
            credentials=pika.credentials.PlainCredentials('guest', 'guest'),
        )
channel = connection.channel()
print(channel.queue_declare(queue="your_queue", durable=True,  exclusive=False,
                  auto_delete=False).method.message_count)

and you will get the exact message number

你会得到确切的消息编号