Pika,从队列中获取所有消息而不使用它们


使用

pika 客户端,我想显示队列中当前的所有消息,而不使用它们。只是为了知道队列有多忙并显示作业。

到目前为止,我只能阅读一条消息:

channel.queue_declare(queue='queue1', durable=True)
channel.basic_consume(on_message, queue='queue1')
channel.start_consuming()
def on_message(channel, method, properties, message):
    channel.basic_ack(delivery_tag=method.delivery_tag)
    print("Message: %s", message)

如何读取整个队列?

要"不消费"地阅读消息,请不要确认消息的传递。在上述情况下,摆脱

channel.basic_ack(delivery_tag=method.delivery_tag)

或将auto_ack设置为 False

def callback(ch, method, properties, body):
    print(body)
   
channel.basic_consume(queue='your_queue', on_message_callback=callback, auto_ack=False)

这些消息将在 rabbitMQ 中被读取并标记为未确认,但仍可在队列中使用。

最新更新