Pika-RabbitMQ-为什么为消费者使用预取计数1时我的交付率大于确认率

问题描述 投票:2回答:1

我在Python中使用RabbitMQ实现PIKA时遇到问题。我想从队列中使用1条消息,对其进行处理,并在工作完成后对其进行确认。然后应该接收下一条消息。

我使用prefetch_count = 1选项,告诉rabbitMQ该使用者一次只需要1条消息,直到确认此消息后才需要新消息。

这是我的代码(非常简单):

credentials = pika.PlainCredentials("username","password")
connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='1.2.3.4', credentials=credentials))
channel = connection.channel()

def consume(ch, method, properties, body):
    time.sleep(5) # Here is the work, now just hold 5 seconds
    ch.basic_ack(method.delivery_tag)

def init():
    channel.basic_consume(
        queue="raw.archive", on_message_callback=consume, auto_ack=False)
    channel.basic_qos(prefetch_count=1)
    channel.start_consuming()

if __name__ == "__main__":
    init()

所以我的问题是,为什么rabbitmq交付的文档(40 /秒)多于已确认的文档(0.20 /秒,正确,因为有5秒钟的暂停)。这两个不应该相等吗?此外,Unacked值(1650)永远不应大于1,因为在确认该文档之前,它不应传送任何文档。

enter image description here

enter image description here第二个视图显示,使用者没有预取计数。但是预取计数是在连接上设置的。也许我必须将其设置给消费者,但是我不知道如何设置它。

我在做什么错?

提前感谢。

python rabbitmq pika
1个回答
0
投票

由Marcel确认,

问题与在通道上设置basic_qos的时间有关。似乎应该在basic_consume之前设置它。

def init():
    channel.basic_qos(prefetch_count=1)
    channel.basic_consume(
        queue="raw.archive", on_message_callback=consume, auto_ack=False)
    channel.start_consuming()
© www.soinside.com 2019 - 2024. All rights reserved.