I use this simple code using pika
:
conn = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = conn.channel()
def on_message(ch, method, properties, message):
print("Received message: %r" % message)
channel.basic_ack(delivery_tag=method.delivery_tag)
channel.queue_declare(queue='my_queue', durable=True)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(on_message, queue='my_queue')
channel.start_consuming()
Question:
Once I got a message, and my on_message
callback is running, how can rabbitmq make sure not to give the same message to other worker, until I call basic_ack
? Since in my tests, if I won't call basic_ack
the message will be sent again if I run the code multiple times. Does rabbitmq has some timeout from the moment it send the message, until it gets an ack
?
how can rabbitmq make sure not to give the same message to other worker, until I call basic_ack ?
Delivering a message to only one consumer is a core feature of RabbitMQ. The message will remain un-acked until the consumer to which it was delivered acks it, or the channel on which it was delivered closes.
in my tests, if I won't call basic_ack the message will be sent again if I run the code multiple times.
When you run your code multiple times, here is what happens:
basic_ack
, which closes the channel and connection without acknowledging the message.All of this and much, much more is covered in the RabbitMQ documentation.