标签:二进制 out receive pip3 channel bsp lock blocking 回调
在生产者和消费者之间加了一个中间件——消息队列,用它可以来干嘛呢?
它是来做消息转发的,当请求过来之后,不是直接发给服务器,而是发给消息队列,然后消息队列把消息中转一下再发给服务器
安装rabbitmq之前先安装erlang
https://www.rabbitmq.com/install-windows.html
#pika是python操作rabbitmq的模块
pip3 install pika
import pika #1.链接到rabbitmq connection = pika.BlockingConnection(pika.ConnectionParameters(‘localhost‘)) channel = connection.channel() #2.创建消息队列并命名 channel.queue_declare(queue=‘hello‘) #3.发送消息 channel.basic_publish(exchange=‘‘, routing_key=‘hello‘, #消息队列名 body=‘Hello World!‘) #要发送的消息 print(" [x] Sent ‘Hello World Done!‘") #4.关闭链接 connection.close()
import pika #1.链接rabbitmq代码 connection = pika.BlockingConnection(pika.ConnectionParameters(‘localhost‘)) channel = connection.channel() #2.队列名 channel.queue_declare(queue=‘hello‘) #3.回调函数:body:要接受的数据为二进制字符串 def callback(ch, method, properties, body): print(" [x] Received %r" % body) channel.basic_consume(queue=‘hello‘, #队列名 auto_ack=True, #告诉消费者已经拿到了数据 on_message_callback=callback) #回调函数 print(‘ [*] Waiting for messages. To exit press CTRL+C‘) #4 channel.start_consuming()
生产者cmd窗口
python3 生产者文件名.py
消费者cmd窗口2
python3 消费者者文件名.py
标签:二进制 out receive pip3 channel bsp lock blocking 回调
原文地址:https://www.cnblogs.com/tfzz/p/12101393.html