Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get total number of consumers for a RabbitMQ queue using pika

The following code is what I use to get a count of number of consumers :

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='IP ADDRESS'))
channel = connection.channel()

this=channel.queue_declare(queue="Queue_name",passive=True)

print this.method.consumer_count

Now the count that I obtain are the number of active consumers. However, when consumers are consuming from the queue, this count is printed as zero. Now I need the total number of consumers consuming from the queue. This appears RabbitMQ Management (as consumers : 0 active 25 Total)

Is there a way to obtain a count of the total number of consumers consuming from a queue, while there are messages in the queue?

Thank you in advance

like image 519
Krsrb1 Avatar asked Nov 24 '25 03:11

Krsrb1


2 Answers

A simple option:

self._channel = self._connection.channel()
queue_state = self._channel.queue_declare(queue=self.__queue_name, passive=True, durable=True)
print(queue_state.method.consumer_count)
print(queue_state.method.message_count)
like image 58
Dorcioman Avatar answered Nov 25 '25 15:11

Dorcioman


Following is an answer to this question. However, it uses HTTP API and not pika.

import subprocess
import os
import json


#Execute in command line
def execute_command(command):
     proc = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) 
     script_response = proc.stdout.read().split('\n')
     resp=json.loads(script_response[7])
     print resp[0]['name']
     print resp[0]['consumers']

  ######### MAIN #########
  if __name__ == '__main__':
      execute_command('curl -i -u guest:guest http://*IP ADDRESS*:15672/api/queues/')

Please refer : http://hg.rabbitmq.com/rabbitmq-management/raw-file/3646dee55e02/priv/www-api/help.html

like image 24
Krsrb1 Avatar answered Nov 25 '25 17:11

Krsrb1