Python&Rabbitmq文档阅读笔记-生产者数据直接送入队列消费者消费
发布日期:2021-06-30 10:46:53
浏览次数:3
分类:技术文章
本文共 2826 字,大约阅读时间需要 9 分钟。
Hello World!
使用Pika库连接到Rabbitmq。
本次要实现的功能:生产者生直接发送消息到队列中,消费者消费队列中的数据。
逻辑结构如下:
Rabbitmq的通信协议有很多,这里使用AMQP 0-9-1,其中python对应的库为Pika 1.0.0
pip安装此库:
python -m pip install pika --upgrade
生产者:
在发送数据之前需要先建立连接
import pikaconnection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))channel = connection.channel()
如果要带用户名和密码,以及virtualHost需要这么写:
credentials = pika.PlainCredentials('userName', 'password')connection = pika.BlockingConnection(pika.ConnectionParameters(host='ipaddress', port=5672, virtual_host="/virtualHost", credentials=credentials))channel = connection.channel()
发消息前要确保队列是存在的,声明一下队列
channel.queue_declare(queue="hello_python")
Rabbitmq其实是不能直接连接队列的,他需要一个交换机,通过交换机去连队列。但在写代码时,将exchange设置为空字符串,他就会走默认的交换机,将routing_key设置为队列的名字,就变成了,直连队列的效果。
代码如下:
channel.basic_publish(exchange='', routing_key='hello_python', body='Hello World')
Rabbitmq其实是不能直接连接队列的,他需要一个交换机,通过交换机去连队列。但在写代码时,将exchange设置为空字符串,他就会走默认的交换机,将routing_key设置为队列的名字,就变成了,直连队列的效果。
代码如下:
channel.basic_publish(exchange='', routing_key='hello_python', body='Hello World')
最后要关闭连接:
connection.close()
下面是消费者:
接收生产者发送的消息,并将消息打印到屏幕上。
首先还是创建连接,以及使用queue_declare确保队列存在
credentials = pika.PlainCredentials('userName', 'password')connection = pika.BlockingConnection(pika.ConnectionParameters(host='ipaddress', port=5672, virtual_host="/virtualHost", credentials=credentials))channel = connection.channel()channel.queue_declare(queue="hello_python")
定义一个回调函数用于消息的接收,如下:
def receiverCallBak(ch, method, properties, body): print("Recived %r" % body) pass
告诉channel,接收到数据后,调用这个回调函数:
channel.basic_consume(queue="hello_python", auto_ack=True, on_message_callback=receiverCallBak)
最后进入循环接收数据:
channel.start_consuming()
程序运行截图如下:
整体代码如下:
import pikadef producer(): credentials = pika.PlainCredentials('userName', 'password') connection = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1', port=5672, virtual_host="/hehe", credentials=credentials)) channel = connection.channel() channel.queue_declare(queue="hello_python") channel.basic_publish(exchange='', routing_key='hello_python', body='Hello World') connection.close() print("send msg over") passdef receiverCallBak(ch, method, properties, body): print("Recived %r" % body) passdef receiver(): credentials = pika.PlainCredentials('userName', 'password') connection = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1', port=5672, virtual_host="/hehe", credentials=credentials)) channel = connection.channel() channel.queue_declare(queue="hello_python") channel.basic_consume(queue="hello_python", auto_ack=True, on_message_callback=receiverCallBak) channel.start_consuming() print("Receiver set finished") passif __name__ == '__main__': for i in range(1, 10): producer() pass receiver() pass
转载地址:https://it1995.blog.csdn.net/article/details/114866859 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月29日 21时35分11秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ranger
2019-04-30
slurm
2019-04-30
xfce4
2019-04-30
xrdp
2019-04-30
Raft算法
2019-04-30
Python计算文本BLEU分数
2019-04-30
swap内存(linux)
2019-04-30
人脸au
2019-04-30
torch.distributed 分布式
2019-04-30
OpenMP编程模型(OMP)
2019-04-30
混合精度训练(FP16 & FP32)
2019-04-30
PyPy
2019-04-30
打印CSDN文章
2019-04-30
MATLAB与CUDA
2019-04-30
Linux png转jpg (convert命令)
2019-04-30
NAS (Network Attached Storage 网络附属存储)
2019-04-30
Ubuntu更新后终端中字体的颜色全是白色
2019-04-30
Ninja
2019-04-30
lmdb数据库的读取与转换(一) —— 基本操作
2019-04-30
opencv相关操作(cv2) (python)
2019-04-30