basic_get之后的pika确认消息

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

我有一个python代码,一次消耗来自RMQ的消息

message_count = queue_state.method.message_count
    if not queue_empty:
        message_cursor = 1
        while message_cursor <= message_count:
            method, properties, body = channel.basic_get(queue=QueueName, auto_ack=True)
            callback(channel, method, properties, body)
            message_cursor += 1

我在回调中对接收到的消息进行了很多解析,并且我得到了一个要求,而不是自动确认消息,仅在它们完成回调后才对其进行确认。我做了一些挖掘,发现有一种叫做basic.ack

的方法。

但是我不确定如何在代码中使用它。该示例使用

channel.basicAck(deliveryTag, false).

我在哪里可以获得deliveryTag的值?我应该在哪里运行basicAck?在我的回调之后还是在回调内部?

非常感谢

python rabbitmq pika
1个回答
1
投票

交付标记在您的方法对象中可用:method.delivery_tag

确实(从我所能看到的)没有在pika文档中出现,但rabbitmq.com上的教程的确提供了example

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    time.sleep( body.count('.') )
    print(" [x] Done")
    ch.basic_ack(delivery_tag = method.delivery_tag)

从引用的示例中检索的示例代码。

© www.soinside.com 2019 - 2024. All rights reserved.