简体   繁体   中英

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..

Turns out its a simple error. Adding

connection.drain_events()

to the while loop causes messages to arrive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM