Get total number of consumers for a RabbitMQ queue using pika

pika, python, python-pika, rabbitmq

Solution

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

Problem

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

Original source