Search code examples
pythonrabbitmqpika

Acknowledging a message inside the consumer_callback in Pika / RabbitMQ


I have a setup where I would like to be able to acknowledge a pika message after a couple of lines inside the consumer_callback and then carry on with some more time intensiv tasks. I have written some code that does exactly this, but it seems, that the acknowledgement only gets sent out after the consumer_callback returns. I'm using pika 10 with the SelectConnection.

I'm wondering if there is a way to achieve this. The methods I've tried so far are:

  • Doing a regular ack in the current callback -> the ack only gets sent out after the callback returns
  • Doing a regular ack via a different connection / channel I create specifically for this case -> fails with "unknown delivery tag"
  • Trying to sneak in a callback via the add_timeout method on the SelectConnection, which then would be called right after the consumer_callback returned -> this somehow messes up the queue communication and very strange things happen, so I'm assuming this is not the right way.

Any help is greatly appreciated. Maybe I need a different connection type?


Solution

  • Upgrade to Pika 0.12.0 and follow the example in this code, or do something similar: link.

    I realize the example using a different connection type, but you get the idea. Pika's ioloop must be running in one thread, and your time intensive task can carry on in another thread.