当前位置:网站首页>Redis 】 【 publish and subscribe message
Redis 】 【 publish and subscribe message
2022-07-31 13:48:00 【icy hope】
1.说明
在Redis2The publish-subscribe function is supported after the version,The publisher creates a channel,and send a message on it,All clients subscribed to the channel will receive the message(不出意外的情况下,但实际不一定),The benefit of publish subscribe is to reduce unnecessary polling,The application scenario has an instant chat room、Public account subscription, etc.但Redis适合小型应用,If it is a large architecture,I believe it will still be usedrabbitMQ或者kafkaWait for more professionalMQqueue software.
Redis-server内部会维护一个字典,键是频道名,The value is a linked list that stores subscribers,Every time a message is published, the linked list is traversed for push.
2.订阅发布
我们打开一个redis终端,使用subscribe命令订阅频道,You may wish to open several terminals and subscribe to the same channel at the same time,For example, I open two terminals here and subscribe to itchatChannel的频道
# 订阅格式:subscribe 频道名
127.0.0.1:6379> subscribe chatChannel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "chatChannel"
3) (integer) 1
再打开一个终端,使用publish命令发布消息,In this way, the terminal that subscribes to the channel will print the result
# 发布格式:publish 频道名 消息
127.0.0.1:6379> publish chatChannel "hello"
(integer) 2
结果如下图
Each client can subscribe to multiple channels at the same time,即subscribeThe parameter specifies multiple channels,另外也可以使用psubscribeCommand to subscribe to multiple channels(Seems to be called a pattern),支持正则表达式
# 同时订阅 频道1、频道2、频道3
subscribe 频道1 频道2 频道3
# 订阅所有以“chat”开头的频道
psubscribe chat*
You can use it if you want to unsubscribeunsubscribe和punsubscribe命令,Usage is a command followed by a channel or matching pattern
3.代码实现
订阅消息,比如说订阅3Unsubscribe after that
from redis import Redis
r = Redis(host='localhost', port=6379, db=0)
p = r.pubsub() # If you don't want to receive some other information, you can put the parameterignore_subscribe_messages改为True
p.subscribe("testChannel") # If you want to subscribe to multiple channels at the same time,可以传入多个参数
# p.psubscribe("test*") # 模式匹配
i = 0
while True:
msg = p.get_message()
if msg:
print("来电了: ", msg)
i += 1
if i > 2:
break
# 取消订阅
p.unsubscribe()
# p.punsubscribe()
p.close()
r.close()
Posting a message is relatively simple
from redis import Redis
r = Redis(host='localhost', port=6379, db=0)
r.publish('testChannel', 'hello everybody')
r.close()
边栏推荐
- 1-hour live broadcast recruitment order: industry leaders share dry goods, and enterprise registration is open丨qubit · point of view
- 网络层重点协议——IP协议
- 报错IDEA Terminated with exit code 1
- 技能大赛训练题:MS15_034漏洞验证与安全加固
- 232层3D闪存芯片来了:单片容量2TB,传输速度提高50%
- csdn发文助手问题
- VU 非父子组件通信
- 动作捕捉系统用于柔性机械臂的末端定位控制
- C#Assembly的使用
- IDEA如何运行web程序
猜你喜欢

ECCV2022: Recursion on Transformer without adding parameters and less computation!

IDEA connects to MySQL database and uses data

【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)

C#使用ComboBox控件

抓住金三银四的尾巴,解锁程序员面试《刷题神器》

技能大赛训练题:域用户和组织单元的创建

ADS与C#通信

C# control StatusStrip use

已解决(pymysqL连接数据库报错)pymysqL.err.ProgrammingError: (1146,“Table ‘test.students‘ doesn‘t exist“)

go使用makefile脚本编译应用
随机推荐
ECCV 2022 | Robotic Interaction Perception and Object Manipulation
尚硅谷-JVM-内存和垃圾回收篇(P1~P203)
Unity学习笔记 关于AVPro视频跳转功能(Seeking)的说明
图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
线程池的使用二
Open Inventor 10.12 重大改进--和谐版
C# using ComboBox control
Spark Learning: Add Custom Optimization Rules for Spark Sql
MATLAB | 我也做了一套绘图配色可视化模板
ECCV 2022 | 机器人的交互感知与物体操作
使用NVM进行node版本切换管理
Text similarity calculation (Chinese and English) detailed explanation of actual combat
Comparison of Optical Motion Capture and UWB Positioning Technology in Multi-agent Cooperative Control Research
SetoolKit使用指南
新款现代帕里斯帝预售开启,安全、舒适一个不落
Usage of += in C#
【蓝桥杯选拔赛真题46】Scratch磁铁游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning
页面整屏滚动效果
技能大赛训练题:ftp 服务攻防与加固