我尝试使用pika来获取RabbitMQ队列中的项目数。
params = pika.ConnectionParameters(host='my.host.com', port=5672, credentials=pika.credentials.PlainCredentials('myuser', 'myauth'))
connection = pika.BlockingConnection(parameters=params)
channel = connection.channel()
response = channel.queue_declare(passive=True, queue='my-queue-name')
count = response.method.message_count
channel.close()
print response
当我运行这个命令时,不管队列中有多少项,count
总是0。我可以看到rabbitmqctl
中存在的项,但是我的脚本不会显示它们。我在这里做错了什么?
1条答案
按热度按时间zfciruhq1#
TL; DR
在声明队列之前添加
channel.basic_qos(prefetch_count=1)
。现在回答可能为时已晚,但我刚刚面临着几乎相同的问题。
我需要对队列的发布者端进行一些节流,并决定定期检查队列大小以降低处理速度,但当我打开队列的消费者端时,pika的
queue_declare(..., passive=True).method.message_count
开始报告零。经过一段时间的测试和浏览/code/samples,我发现设置
channel.basic_qos(prefetch_count=1)
解决了这个问题。希望这个有用。