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

Krs*_*rb1 2 python rabbitmq pika python-pika

以下代码是我用来计算消费者数量的代码:

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
Run Code Online (Sandbox Code Playgroud)

现在我获得的数量是活跃消费者的数量。但是,当消费者从队列中消费时,此计数将打印为零。现在我需要从队列中消费的消费者总数。这出现了 RabbitMQ 管理(作为消费者:0 活动 25 总计)

有没有办法在队列中有消息时获取从队列中消费的消费者总数的计数?

先感谢您

Krs*_*rb1 5

以下是对这个问题的回答。但是,它使用 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/')
Run Code Online (Sandbox Code Playgroud)

请参考:http : //hg.rabbitmq.com/rabbitmq-management/raw-file/3646dee55e02/priv/www-api/help.html


小智 5

一个简单的选择:

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)
Run Code Online (Sandbox Code Playgroud)