每个回合处理一条来自队列的消息

2024-09-29 21:45:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要拉取每一条消息,处理它,完成pull and next之后,因为我要向队列发送许多消息,而接收者很少,但是我希望每个接收者接受一条消息,处理它,然后拉取下一条消息。在

我不明白如何用这段代码实现这一点:

def main():
credentials = pika.PlainCredentials('admin', 'admin')
connection = pika.BlockingConnection(pika.ConnectionParameters(host=ConfigurationManager.ConfigurationManager().getMessageBrokerUrl(), credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue=matchingExec.getChannelName(), durable=True)

def callback(ch, method, properties, body):
    matchingExec.handleMatchingMessage(json.loads(body))

channel.basic_qos(1)    
channel.basic_consume(callback,
                      queue=matchingExec.getChannelName(),
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

Tags: true消息adminqueuedefcallbackchannelbody
1条回答
网友
1楼 · 发布于 2024-09-29 21:45:22

不知道是什么问题,因为你没有在你的问题中描述它,有一件事绝对奇怪,那就是你如何设置你的qos。在

如果你这样做,你的问题会消失吗:

channel.basic_qos(prefetch_count=1)

现在只需将其设置为1,而不使用keyword参数,根据文档,第一个参数可能是prefetch_size而不是prefetch_count。在

但我不能百分之百的肯定你能做到这一点没有消息确认。当您将其作为no_ack=True运行时,rabbit会在每个消息被发送到客户机时立即隐式地确认它们,并且服务器无法判断消息是否已经被客户机处理过。这可能会使基本的服务质量在理论上没有任何效果,因为服务器不会从客户端接收任何反馈。在

如果仍然不起作用,请尝试打开消息确认,并在处理完成后在回调中确认消息。在

相关问题 更多 >

    热门问题