pythonconcurrencyrabbitmqrace-conditionpika

Can RabbitMQ used in a Python consumer (via pika) have race conditions between concurrently received messages?


Suppose we have the following simple RabbitMQ callback logic in Python:

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password")))
channel = connection.channel()

msg_count = 0

def callback(ch, method, properties, body):
    global msg_count
    msg_count += 1
    print("Received %i messages" % msg_count)

channel.basic_consume(queue="my_queue", on_message_callback=callback, auto_ack=True)

If a producer sends two messages practically at the same time, is it possible that "Received 1 messages" will be printed twice due to a race condition?

I'm just not sure how these callbacks run - are they run concurrently? In a thread? Some other way?


Solution

  • If a producer sends two messages practically at the same time, is it possible that "Received 1 messages" will be printed twice due to a race condition?

    No. Pika runs an I/O loop internally and events are dealt with as they are ready, sequentially.