Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Kombu consumer not notified of rabbitmq message (queue.get does work)

If I run the following code, the callback (test) passed to the consumer is never triggered.

However, if I keep an eye on the rabbitmq GUI, I do see that the message is retrieved (but not acknowledged). So it seems the consumer is getting the message, but not passing it on to my callback. If I set no_ack to true, the message just disappears from the queue, again without calling the callback.

hn = "..."
usr = "..."
pwd = "..."
vh = "/"
port = 5672
rkey = "some.routing.key"
qname = "some-queue-name"
exchangeName = "MyExchange"

connection = BrokerConnection(hostname=hn,
                              userid=usr,
                              password=pwd,
                              virtual_host=vh,
                              port=port)

connection.connect()
ch = connection.channel()

# Create & the exchange
exchange = Exchange(name=exchangeName,
              type="topic",
              channel=ch,
              durable=True)

exchange.declare()

# Temporary channel
ch = connection.channel()

# Create the queue to feed from
balq = Queue(name=qname,
              exchange=exchange,
              durable=True,
              auto_delete=False,
              channel=ch,
              routing_key=rkey)        

# Declare it on the server
balq.declare();

def test(b,m):
    print '** Message Arrived **'

# Create a consumer
consumer = Consumer(channel=connection.channel(),
                    queues=balq,
                    auto_declare=False,
                    callbacks = [test]
                    )

# register it on the server
consumer.consume(no_ack=False);

print 'Waiting for messages'
while(True):
    pass

However, the following code does work properly (I can successfully get and acknowledge the message):

m = balq.get(no_ack=False)
m.ack()
print m

But the whole point was to stay asynchronous. So something must be wrong with my callback..

like image 938
dgorissen Avatar asked Mar 08 '11 17:03

dgorissen


People also ask

Does RabbitMQ guarantee message order?

Message Ordering The RabbitMQ documentation states the following regarding its ordering guarantees: Messages published in one channel, passing through one exchange and one queue and one outgoing channel will be received in the same order that they were sent.

What is kombu in Python?

`Kombu` is a messaging library for Python. The aim of `Kombu` is to make messaging in Python as easy as possible by. providing an idiomatic high-level interface for the AMQ protocol, and also. provide proven and tested solutions to common messaging problems.

How RabbitMQ consumer works?

RabbitMQ is a messaging broker. It accepts messages from publishers, routes them and, if there were queues to route to, stores them for consumption or immediately delivers to consumers, if any. Consumers consume from queues. In order to consume messages there has to be a queue.

Is RabbitMQ round robin?

By default, RabbitMQ will send each message to the next consumer, in sequence. On average every consumer will get the same number of messages. This way of distributing messages is called round-robin.


1 Answers

Turns out its a simple error. Adding

connection.drain_events()

to the while loop causes messages to arrive.

like image 138
dgorissen Avatar answered Sep 24 '22 03:09

dgorissen