Using Python Pika to get queue counts

3,325 views
Skip to first unread message

matt.s...@fanmanager.com

unread,
Jan 15, 2016, 11:04:21 AM1/15/16
to rabbitmq-users

Below is a bit of code that I found somewhere on how to query Rabbit using Python to get the queue message count.  I have a process that uses this and tries to determine when the queues are clear, then it powers off the servers (only runs on dev servers).  Before this runs, the process that adds new messages is shut off, we've verified no new messages are being added.  The problem I'm having is it "seems" to be getting the correct message count, but I've had a complaint that a few times the server was shutdown with a possible unack'd message.


Does message_count show *all* messages, or only ready messages?


servername = "mq001"
params = pika.ConnectionParameters(
host=servername,
port=port,
virtual_host=virtual_host,
credentials=pika.PlainCredentials(username,password),
)
connection = pika.BlockingConnection(params)
channel = connection.channel()
status = channel.queue_declare(queue=queue_name, durable=True)
count = status.method.message_count

Michael Klishin

unread,
Jan 15, 2016, 11:16:19 AM1/15/16
to rabbitm...@googlegroups.com, matt.s...@fanmanager.com
On 15 January 2016 at 19:04:23, matt.s...@fanmanager.com (matt.s...@fanmanager.com) wrote:
> Does message_count show *all* messages, or only ready messages?

Only those in Ready state.

The reason for this is simple: a client that gets the count might then choose to
consume exactly that number of messages, or pre-allocate resources, etc.

If there is a huge number of unacknowledged messages, we don't want them to assume
there's more than they really can consume.

Use HTTP API to get the breakdown you see in the management UI (ready/unacked/total). 
--
MK

Staff Software Engineer, Pivotal/RabbitMQ


Matt Shields

unread,
Jan 15, 2016, 11:17:18 AM1/15/16
to Michael Klishin, rabbitm...@googlegroups.com
Is there a way to programmatically get it, either via Pika or another Python library?

Matthew Shields
FanManager
Cell:     +1 781.424.3531
Skype: mattshields74

Michael Klishin

unread,
Jan 15, 2016, 11:21:26 AM1/15/16
to Matt Shields, rabbitm...@googlegroups.com
On 15 January 2016 at 19:17:07, Matt Shields (matt.s...@fanmanager.com) wrote:
> Is there a way to programmatically get it, either via Pika or
> another Python library?

Not via Pika but certainly you can put together a small HTTP API clients on top of,
say, Requests.

See
https://raw.githack.com/rabbitmq/rabbitmq-management/rabbitmq_v3_6_0/priv/www/api/index.html

which is available at /api on any management plugin instance.

If you need an example of such client, take a look at https://github.com/ruby-amqp/rabbitmq_http_api_client, it's
the closest thing I know of to Python. There are also Java and Go clients.

Back to your original question,  you want GET /api/queues/{vhost}/{name}.
Reply all
Reply to author
Forward
0 new messages