consumer_priority.py 794 B

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