Python通过RabbitMQ服务器实现交换机功能的实例教程
作者:陈杰斌 时间:2023-08-24 01:15:19
快速回顾一下RabbitMQ服务器的安装:
sudo apt-get install rabbitmq-server
Python使用RabbitMQ需要Pika库:
sudo pip install pika
好了,接下来我们先看交换机的工作原理:消息发送端先将消息发送给交换机,交换机再将消息发送到绑定的消息队列,而后每个接收端都能从各自的消息队列里接收到信息。
下面用send.py和receive.py来模拟实现交换机的功能。send.py表示发送端,receive.py表示接收端。
receive.py:
#!/usr/bin/env python
#coding=utf8
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
#定义交换机
channel.exchange_declare(exchange='messages', type='fanout')
#随机生成队列,并绑定到交换机上
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='messages', queue=queue_name)
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
channel.basic_consume(callback, queue=queue_name, no_ack=True)
print ' [*] Waiting for messages. To exit press CTRL+C'
channel.start_consuming()
上例代码中,queue_declare的参数exclusive=True表示当接收端退出时,销毁临时产生的队列,这样就不会占用资源。运行这个程序,然后使用rabbitmqctl list_exchanges命令来查看交换机信息:
其中红色框就是上例定义的交换机了。再使用rabbitmqctl list_queues查看下消息队列情况:
其中红色框就是随机产生的消息队列了。
send.py:
#!/usr/bin/env python
#coding=utf8
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
#定义交换机
channel.exchange_declare(exchange='messages', type='fanout')
#将消息发送到交换机
channel.basic_publish(exchange='messages', routing_key='', body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()
上例代码中, basic_publish方法的参数exchange被设定为相应交换机,因为是要广播出去,发送到所有队列,所以routing_key就不需要设定了。
exchange如果为空,表示是使用匿名的交换机,在上面交换机信息的图片中可以看到有amq.*这样的交换机,就是系统默认的交换机了。routing_key在使用匿名交换机的时候才需要指定,表示发送到哪个队列的意思。第一篇的例子演示了这个功能。
打开另外一个终端,执行send.py,可以观察到receive.py接收到了消息。如果有多个终端执行receive.py,那么每个receive.py都会接收到消息。
标签:Python,RabbitMQ
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django使用原生SQL查询数据库详解
2024-01-21 00:58:08
python实现某考试系统生成word试卷
2022-05-20 18:29:09
![](https://img.aspxhome.com/file/2023/1/101561_0s.jpg)
mysql Innodb表空间卸载、迁移、装载的使用方法
2024-01-25 16:41:56
python tkinter基本属性详解
2021-11-22 06:18:35
![](https://img.aspxhome.com/file/2023/2/93702_0s.png)
详解python进行mp3格式判断
2023-11-06 14:08:20
Microsoft .Net Remoting系列教程之一:.Net Remoting基础篇
2024-05-09 09:03:35
![](https://img.aspxhome.com/file/2023/2/129812_0s.jpg)
Python中json库的操作指南
2021-10-11 11:04:43
Web设计中的黄金分割[译]
2009-02-20 13:41:00
![](https://img.aspxhome.com/file/UploadPic/20092/20/01-25s.jpg)
微信小程序自定义导航教程(兼容各种手机)
2024-04-30 09:53:12
![](https://img.aspxhome.com/file/2023/4/132564_0s.png)
用好FrontPage2003的九大功能
2008-02-21 14:29:00
![](https://img.aspxhome.com/file/UploadPic/20082/21/2008221143234106s.jpg)
python没有gpu,如何改用cpu跑代码
2022-09-26 00:24:49
![](https://img.aspxhome.com/file/2023/7/101137_0s.png)
Python获取脚本所在目录的正确方法
2022-12-07 14:37:36
基于Python编写一个刷题练习系统
2022-10-01 13:28:34
![](https://img.aspxhome.com/file/2023/2/79442_0s.png)
神经网络训练采用gpu设置的方式
2021-04-24 16:43:25
![](https://img.aspxhome.com/file/2023/8/135118_0s.jpg)
2022年最新Typora的破解方法
2023-08-29 04:36:03
![](https://img.aspxhome.com/file/2023/5/127205_0s.jpg)
mysql8.0.30安装配置最详细教程(windows 64位)
2024-01-16 10:01:35
![](https://img.aspxhome.com/file/2023/8/129928_0s.jpg)
基于Python实现通过微信搜索功能查看谁把你删除了
2022-07-10 00:19:37
![](https://img.aspxhome.com/file/2023/2/79752_0s.jpg)
Python学习之列表常用方法总结
2021-11-24 18:43:19
SQLite3数据库的介绍和使用教程(面向业务编程-数据库)
2024-01-28 20:40:07
Python实现的生产者、消费者问题完整实例
2022-08-15 14:37:30