How to count the number of messages in a RabbitMQ queue using Python ?

12,888 views
Skip to first unread message

Anurag

unread,
Oct 15, 2014, 4:08:28 PM10/15/14
to rabbitm...@googlegroups.com
I would like to find out the total number of messages in a queue and compare it against a value using Python. How can I do this?

Thanks

Gavin M. Roy

unread,
Oct 15, 2014, 4:27:39 PM10/15/14
to Anurag, rabbitm...@googlegroups.com

import rabbitpy

with rabbitpy.Connection('amqp://guest:guest@localhost:15672') as conn:
    with conn.channel() as channel:
        queue = rabbitpy.Queue(channel, 'queue-name')
        print('The queue has {0} messages'.format(len(queue)))


import pika

connection = pika.BlockingConnection()
channel = connection.channel()
response = channel.queue_declare(‘queue-name’, passive=True)
print('The queue has {0} messages'.format(response.message_count))

With requests:

import requests

response = requests.get('http://guest:guest@localhost:15672/api/queues/%2f/queue-name')
data = response.json()
print('The queue has {0} messages'.format(data['messages']))

Regards,

Gavin  







On Wed, Oct 15, 2014 at 4:08 PM, Anurag <anuragpa...@gmail.com> wrote:

I would like to find out the total number of messages in a queue and compare it against a value using Python. How can I do this?

Thanks

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Klishin

unread,
Oct 15, 2014, 4:28:51 PM10/15/14
to rabbitm...@googlegroups.com, Anurag
 On 16 October 2014 at 00:08:33, Anurag (anuragpa...@gmail.com) wrote:
> I would like to find out the total number of messages in a queue
> and compare it against a value using Python. How can I do this?

1. Execute queue.declare (possibly with passive = True) and see message count on
queue.declare-ok.
2. Use HTTP API.

The former is what I'd recommend.
--
MK

Staff Software Engineer, Pivotal/RabbitMQ

Laing, Michael

unread,
Oct 15, 2014, 4:29:09 PM10/15/14
to Anurag, rabbitm...@googlegroups.com
I use python pika and get that from a queue declare ok.

You can also get it from basic get and maybe other places - see http://www.rabbitmq.com/amqp-0-9-1-reference.html and search for message-count.

ml

On Wed, Oct 15, 2014 at 4:08 PM, Anurag <anuragpa...@gmail.com> wrote:
I would like to find out the total number of messages in a queue and compare it against a value using Python. How can I do this?

Thanks

--

Pythonjunkie

unread,
Oct 21, 2014, 2:20:36 PM10/21/14
to rabbitm...@googlegroups.com
I am using the following code to print the number of current messages in a queue and the body of the message. 
But instead of getting the current number, I only get the inital number of messages in the queue (Let's say 10)
So rather than printing:
10
"msg1"
9
"msg2"
...
...
1
"msg10"

I get:

10
"msg1"
10
"msg2"
...
...
1
"msg10"

Here is my code.

def create_channel():
credentials = pika.PlainCredentials("guest", "guest")
conn_params = pika.ConnectionParameters("localhost",credentials = credentials)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
channel.queue_declare(queue='q1',passive=True)
global count
count = channel.queue_declare(queue='q1',passive=True)
channel.queue_bind(exchange='q', queue='q1', routing_key='q1')
return channel


def consume_run_loop():
channel = create_channel()
def msg_consumer(channel, method, header, body):
a = count.method.message_count
print a
while a:
channel.basic_ack(delivery_tag=method.delivery_tag)
print body
now = time.time()
print "Job Completed in: %s seconds" % round(time.time(), 2)
time.sleep(5)
return
channel.basic_consume( msg_consumer, queue="q1")

consume_run_loop()

How do I print the update the number of messages in the queue?
Thanks

Michael Klishin

unread,
Oct 21, 2014, 2:33:02 PM10/21/14
to rabbitm...@googlegroups.com, Pythonjunkie
On 21 October 2014 at 22:20:43, Pythonjunkie (anuragpa...@gmail.com) wrote:
> a = count.method.message_count

I'm not a Python expert but the `count` value seems to be constant. You have to
use a queue.declare  for every delivery.

Simon MacMullen

unread,
Oct 21, 2014, 2:36:46 PM10/21/14
to rabbitm...@googlegroups.com, Pythonjunkie, Michael Klishin
On 21/10/2014 19:32, Michael Klishin wrote:
> You have to use a queue.declare for every delivery.

Although if you're going to do that, you might as well use basic.get
(which will also tell you the number of messages in the queue).

But in general the idea is that you shouldn't have to care how many
messages there are, as a consumer. You just process them as they come in.

Cheers, Simon

Pythonjunkie

unread,
Oct 21, 2014, 3:06:11 PM10/21/14
to rabbitm...@googlegroups.com, anuragpa...@gmail.com
When I use 'queue_declare' inside my msg_consumer() function, I get the following error:
"KeyError: 'Queue.DeclareOk' "
 

Michael Klishin

unread,
Oct 21, 2014, 3:11:24 PM10/21/14
to rabbitm...@googlegroups.com, Pythonjunkie
On 21 October 2014 at 23:06:17, Pythonjunkie (anuragpa...@gmail.com) wrote:
> When I use 'queue_declare' inside my msg_consumer() function,
> I get the following error:
> "KeyError: 'Queue.DeclareOk' "

There should be no reason why queue.declare can't be used inside a delivery handler.
Certainly the protocol allows it.

So check your code, you are trying to get a key that does not exist on a dictionary.

Pythonjunkie

unread,
Oct 21, 2014, 3:26:41 PM10/21/14
to rabbitm...@googlegroups.com, anuragpa...@gmail.com
But I only get that error when I use 'queue.declare' inside the function

Michael Klishin

unread,
Oct 21, 2014, 5:08:27 PM10/21/14
to rabbitm...@googlegroups.com, Pythonjunkie
 On 21 October 2014 at 23:26:46, Pythonjunkie (anuragpa...@gmail.com) wrote:
> But I only get that error when I use 'queue.declare' inside the
> function

This may be a non-intentional client library limitation. There is nothing in RabbitMQ
or the protocol that limits when queue.declare can be used.

As Simon suggested, you either should not care about how many messages there are
in the queue, or consider using basic.get instead, where the response indicates
if there was a message or not.
Reply all
Reply to author
Forward
0 new messages