Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pika acknowledge message after basic_get

I have a python code which consumes messages from RMQ one at a time

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

I did a lot of parsing of received messages in the callback, and I get a requirement to instead of auto-acknowledge messages, only acknowledge them after them finish call back. I did some digging and found out there is a method called basic.ack

But I am not sure how to use it in the code. the example is using

channel.basicAck(deliveryTag, false).

Where can I get the value of the deliveryTag? and where should I run the basicAck? right after my callback or inside the callback?

Thanks very much

like image 630
milton Avatar asked Aug 31 '26 10:08

milton


1 Answers

Delivery tag is available in your method object: method.delivery_tag

It is indeed not (from what I can see) present in the pika documentation, but the tutorials on rabbitmq.com do give the 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)

sample code retrieved from the example referenced.

like image 158
Olivier Avatar answered Sep 02 '26 11:09

Olivier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!