统一消息系统在师范大学中的应用与实现
# 生产者代码
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='unified_message')
channel.basic_publish(exchange='',
routing_key='unified_message',
body='这是来自师范大学的通知')
print(" [x] 发送成功")
connection.close()
# 消费者代码
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='unified_message')
def callback(ch, method, properties, body):
print(" [x] 收到:", body.decode())
channel.basic_consume(callback,
queue='unified_message',
no_ack=True)
print(' [*] 等待消息。按 Ctrl+C 退出')
channel.start_consuming()
本站知识库部分内容及素材来源于互联网,如有侵权,联系必删!