当前位置:网站首页>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()
边栏推荐
- Buffer 与 拥塞控制
- C#获得网卡信息 NetworkInterface IPInterfaceProperties
- What should I do if selenium is reversed?
- C# List用法 List介绍
- The latest complete code: Incremental training using the word2vec pre-training model (two loading methods corresponding to two saving methods) applicable to various versions of gensim
- Grab the tail of gold, silver and silver, unlock the programmer interview "Artifact of Brushing Questions"
- 关于MySQL主从复制的数据同步延迟问题
- 49.【拷贝构造函数与重载】
- IDEA connects to MySQL database and uses data
- LeetCode只出现一次的数字
猜你喜欢
拥塞控制,CDN,端到端
机器学习模型验证:被低估的重要一环
Miller_Rabin Miller Rabin probability sieve [template]
The Selenium IDE of the Selenium test automation
hyperf的启动源码分析(二)——请求如何到达控制器
C# control ToolStripProgressBar usage
AWS实现定时任务-Lambda+EventBridge
EXCEL如何快速拆分合并单元格数据
爱可可AI前沿推介(7.31)
Reasons and solutions for Invalid bound statement (not found)
随机推荐
leetcode:485.最大连续 1 的个数
尚硅谷-JVM-内存和垃圾回收篇(P1~P203)
ICML2022 | 面向自监督图表示学习的全粒度自语义传播
ECCV 2022 | 机器人的交互感知与物体操作
The importance of strategic offensive capability is much higher than strategic defensive capability
页面整屏滚动效果
Resolved (pymysqL connect to the database error) pymysqL. Err. ProgrammingError: (1146, "Table" test. Students' doesn 't exist ")
numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
IDEA找不到Database解决方法
Open Inventor 10.12 重大改进--和谐版
Six Stones Programming: No matter which function you think is useless, people who can use it will not be able to leave, so at least 99%
LeetCode rotate array
A detailed explanation of the usage of Async and Await in C#
Miller_Rabin 米勒拉宾概率筛【模板】
Batch大小不一定是2的n次幂!ML资深学者最新结论
leetcode:2032. 至少在两个数组中出现的值
生产力工具和插件
Grab the tail of gold, silver and silver, unlock the programmer interview "Artifact of Brushing Questions"
关于MySQL主从复制的数据同步延迟问题
[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)