简体   繁体   中英

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM