Search code examples
pythonrabbitmqpika

Pika example https://github.com/pika/pika/blob/main/examples/basic_consumer_threaded.py


I have read some post in this group and I don’t understand what is the point of using: https://github.com/pika/pika/blob/main/examples/basic_consumer_threaded.py

I mean, why we need one additional thread for processing message and calling add_callback_threadsafe where as argument we pass ack method (both activities in the same new thread for processing). I wonder how we prevent form closing connection from channel with we consume message, because if processing is longer than heartbeat value we send ack afterwards and it is for me the same case like just consuming in the main thread and then sending ack. What is the difference if we run processing and sending ack in separate one thread?

I am new in python and pika so sorry if this question is strange

I don't understand, I want explanation


Solution

  • if processing is longer than heartbeat value we send ack afterwards and it is for me the same case like just consuming in the main thread and then sending ack

    Nope, that's not true. If you consume in the main thread, the time it takes to process the message prior to sending the ack BLOCKS Pika's I/O loop, because Pika's I/O loop runs on whatever thread created the connection (i.e. the main thread). This is why you should do work on a separate thread.

    Here is a line-by-line explanation of some example code: https://groups.google.com/g/rabbitmq-users/c/grHdO0s6Owo/m/F6ULK0G0BgAJ

    I've answered quite a few of these "how does Pika work with threads" questions lately and they all come down to a mis-understanding of threading, Pika and it's I/O loop. Pika does NOT start a separate thread for I/O. If you do work in the thread that you create the Pika connection on, you will block the I/O loop.

    Note that, in general, any program or library that does I/O does so in a loop, which will block whatever thread on which the loop is created. Some libraries create a dedicated thread for this and "hide" it from the user, Pika does not.