import pika
import time
# 连接到RabbitMQ服务器
credentials = pika.PlainCredentials('xxxxxxxxxxxxxxx==',
'xxxxxxxxxxxxxxxxxxxxxxxx')
connection = pika.BlockingConnection(pika.ConnectionParameters(
'rabbitmq-serverless-cn-y1i3ptrzv06.cn-hangzhou.amqp-11.net.mq.amqp.aliyuncs.com', 5672, virtual_host='zhgj', credentials=credentials))
# connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
queue_name = "test"
channel.queue_declare(queue=queue_name, durable=True, passive=True)
channel.basic_qos(prefetch_count=1) # 最多处理一个信息,处理完再接收
def callback(ch, method, properties, body): # 回调函数
print("ch:", ch)
print("method:", method)
print("properties:", properties)
print("接收到信息:", body)
time.sleep(30)
# ch.basic_ask(delivery_tag=method.delivery_tag) # 消息确认
channel.basic_consume(queue_name, on_message_callback=callback, # 如果收到消息就调用函数处理消息
auto_ack=False) # acknowledgement确认
print("waiting for message, ctrl+c break")
# 启动接收
channel.start_consuming()