简体   繁体   English

Python Kombu消费者未通知rabbitmq消息(queue.get确实有效)

[英]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). 但是,如果我密切关注rabbitmq GUI,我确实会看到该消息被检索(但未被确认)。 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. 如果我将no_ack设置为true,则消息将从队列中消失,同样不再调用回调。

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. 到while循环导致消息到达。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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