简体   繁体   English

使用 RabbitMQ 的 pika 确认消息

[英]Acknowledge messages with pika for RabbitMQ

I'm using RabbitMQ for message exchange between different docker containers.我正在使用 RabbitMQ 在不同的 docker 容器之间进行消息交换。 On the client side I use pika for communication with RabbitMQ.在客户端,我使用 pika 与 RabbitMQ 进行通信。 Now the following problem appeared:现在出现了以下问题:

Using BlockingConnection I receive a message in my consumer which triggers a quite time consuming task.使用 BlockingConnection 我在我的消费者中收到一条消息,该消息触发了一项非常耗时的任务。 During processing of this task RabbitMQ closed the connection because the heartbeat wasn't sent from the consuming connection as it's busy processing the task.在此任务的处理过程中,RabbitMQ 关闭了连接,因为心跳没有从消费连接发送,因为它正忙于处理任务。

The solution I came up to deal with this is that I shutoff the automatic acknowledgment in pika when attaching a callback to the consumer queue like this channel.basic_consume(queue='myqueue', on_message_callback=my_callback, auto_ack=False) .我想出的解决方案是在将回调附加到消费者队列时关闭 pika 中的自动确认,例如channel.basic_consume(queue='myqueue', on_message_callback=my_callback, auto_ack=False) Additionally I moved the time consuming processing to a daemonized python thread in order to allow the BlockingConnection to send the heartbeat required by RabbitMQ.此外,我将耗时的处理移至守护进程的 Python 线程,以允许 BlockingConnection 发送 RabbitMQ 所需的心跳。

Now I need to acknowledge the RabbitMQ message from the thread once the processing is finished.现在我需要在处理完成后确认来自线程的 RabbitMQ 消息。 For that I extract the delivery_tag from the method as well as the channel_number from the channel property provided by pika.为此,我从method提取了delivery_tag ,并从 pika 提供的channel属性中提取了channel_number

channel_number = channel.channel_number
delivery_tag = method.delivery_tag

Now in the thread I create a new connection as pika is not threadsafe现在在线程中我创建了一个新连接,因为 pika 不是线程安全的

conn = BlockingConnection(ConnectionParameters(host="rabbitmq"))
channel = conn.channel(channel_number=channel_number)
channel.queue_declare(queue='myqueue', durable=False)
channel.basic_ack(delivery_tag=delivery_tag)
conn.close()

to acknowledge the message.确认消息。

However the acknowleging doesn't work as pika yields this warnings:但是,确认不起作用,因为 pika 会产生以下警告:

WARNING:pika.channel:Received remote Channel.Close (406): 'PRECONDITION_FAILED - unknown delivery tag 1' on <Channel number=1 CLOSING conn=<SelectConnection OPEN transport=<pika.adapters.utils.io_services_utils._AsyncPlaintextTransport object at 0x7f94f1dad9e8> params=<ConnectionParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>>
WARNING:pika.adapters.blocking_connection:Got ChannelClosed while closing channel from connection.close: ChannelClosedByBroker: (406) 'PRECONDITION_FAILED - unknown delivery tag 1'

Q1: Is it possible to acknowledge a message from connection B which was received by connection A? Q1:是否可以确认连接 A 收到的来自连接 B 的消息?

Q2: Any idea why this is not working? Q2:知道为什么这不起作用吗?

I read about rabbitpy and AMQPStorm and try those now我阅读了rabbitpyAMQPStorm并立即尝试

使用示例后的add_callback_threadsafe函数解决了该add_callback_threadsafe

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

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