简体   繁体   English

使用pika获取RabbitMQ队列的消费者总数

[英]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) 这将显示RabbitMQ管理(作为消费者:0有效25个总计)

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. 但是,它使用HTTP API,而不使用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 请参考: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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