gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:50:01 36 4
gpt4 key购买 nike

如果我运行以下代码,回调(测试)传递给 consumer永远不会被触发。

但是,如果我留意 rabbitmq GUI,我会看到消息已被检索(但未确认)。所以看起来消费者正在收到消息,但没有将其传递给我的回调。如果我将 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

但重点是保持异步。所以我的回调一定有问题..

最佳答案

原来这是一个简单的错误。添加

connection.drain_events()

while 循环导致消息到达。

关于Python Kombu 消费者未收到 rabbitmq 消息通知(queue.get 有效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5236195/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com