pika总是显示RabbitMQ队列大小为0

j8ag8udp  于 2022-12-29  发布在  RabbitMQ
关注(0)|答案(1)|浏览(204)

我尝试使用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中存在的项,但是我的脚本不会显示它们。我在这里做错了什么?

zfciruhq

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)解决了这个问题。
希望这个有用。

相关问题