# -*- coding: utf-8 -*-
import pika
import time

user_info = pika.PlainCredentials('user', 'J70e6K7BRrxrU1dO')
params = pika.ConnectionParameters('rabbitmq.rabbitmq.svc.cluster.local', 5672, '/', user_info)
conn = pika.BlockingConnection(params)
ch = conn.channel()

# 1. 声明惰性队列
ch.queue_declare(
    queue='lazy_queue',
    durable=True,
    arguments={'x-queue-mode': 'lazy'}  # 这就是全部魔法
)

print(' [*] Waiting messages on lazy_queue (Ctrl+C to quit)')

def callback(ch, method, props, body):
    print(' [x] Received:', body.decode())
    time.sleep(1)        # 模拟慢速消费
    ch.basic_ack(method.delivery_tag)

ch.basic_qos(prefetch_count=10)
ch.basic_consume(queue='lazy_queue', on_message_callback=callback)
ch.start_consuming()