1

From the description in the pika documentation, I can't quite get what add_callback_threadsafe() method does. It says, "Requests a call to the given function as soon as possible in the context of this connection’s thread". Specifically, which event does this callback get associated to? Since, add_callback_threadsafe() doesn't receive any "event" argument, how does pika know when to invoke that callback?

Also, in the official example, why do we even need to build the partial function and register the callback in the do_work() method? Could we not just call the ack_message() method after the time.sleep() is over?

sherlock
  • 2,397
  • 3
  • 27
  • 44

1 Answers1

1

The reason why the method exists: How to add multiprocessing to consumer with pika (RabbitMQ) in python

If you use the same rabbit connection/channel with multiple threads/processes then you'll be prone to crashes. If you want to avoid that, you need to use that method.

Could we not just call the ack_message() method after the time.sleep() is over?

No, you would be calling ack_message from a different thread than the main one, that's not thread safe and prone to crashes. You need to call ack_message from a thread-safe context, i.e., the add_callback_threadsafe().

Christian Pao.
  • 484
  • 4
  • 13