Jed Estep
Jed Estep

Reputation: 605

pika always shows a RabbitMQ queue size of 0

I'm trying to use pika to get the number of items in my RabbitMQ queue. I have the following running:

params = pika.ConnectionParameters(host='my.host.com', port=5672, credentials=pika.credentials.PlainCredentials('myuser', 'myauth'))
connection = pika.BlockingConnection(parameters=params)
channel = connection.channel()
response = channel.queue_declare(passive=True, queue='my-queue-name')
count = response.method.message_count
channel.close()
print response

When I run this, count is always 0 regardless of how many items are in the queue. I can see items present with rabbitmqctl but my script won't show them. What am I doing wrong here?

Upvotes: 4

Views: 1526

Answers (1)

alexkrus
alexkrus

Reputation: 39

TL;DR

add channel.basic_qos(prefetch_count=1), before declare queue.


It may be too late to answer but I just faced with almost the same problem.

I needed to made some throttling on publisher's side of the queue and I decided to periodically check the queue size to slow down processing. But when I brought up consumer's end of the queue pika's queue_declare(..., passive=True).method.message_count starting to report zero.

After some time of testing and browsing /code/samples I found that setting channel.basic_qos(prefetch_count=1) solves the problem.

Hope this helps.

Upvotes: 1

Related Questions