| 1234567891011121314151617181920212223242526 |
- # -*- coding: utf-8 -*-
- import pika
- user_info = pika.PlainCredentials('user', 'Un2yzriWm7veSDoh')
- connection = pika.BlockingConnection(
- pika.ConnectionParameters('rabbitmq.rabbitmq.svc.cluster.local', 5672, '/', user_info)
- )
- channel = connection.channel()
- # 1. 声明同名队列(幂等)
- channel.queue_declare(
- queue='priority_queue',
- durable=True,
- arguments={'x-max-priority': 10}
- )
- print(' [*] Waiting for messages. Higher priority comes first.')
- # 2. 回调
- def callback(ch, method, properties, body):
- print(f" [x] Received priority={properties.priority}: {body.decode()}")
- ch.basic_ack(delivery_tag=method.delivery_tag)
- channel.basic_qos(prefetch_count=1)
- channel.basic_consume(queue='priority_queue', on_message_callback=callback)
- channel.start_consuming()
|