Python Kombu消费者未收到RabbitMQ消息(queue.get正常工作)
如果我运行下面的代码,传给 消费者 的回调函数(test)从来没有被触发。
不过,如果我查看rabbitmq的图形界面,我确实看到消息被取走了(但没有被确认)。所以看起来消费者是收到了消息,但没有把它传给我的回调函数。如果我把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
但是,下面的代码可以正常工作(我可以成功获取并确认消息):
m = balq.get(no_ack=False)
m.ack()
print m
但我最开始的目的是保持异步。所以我的回调函数肯定是哪里出了问题……
1 个回答
5
结果发现这是一个简单的错误。添加
connection.drain_events()
到这个循环里,就能让消息正常到达了。