当前位置:网站首页>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()
边栏推荐
- All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
- 1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
- ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning
- 报错:npm ERR code EPERM
- csdn发文助手问题
- Miller_Rabin Miller Rabin probability sieve [template]
- C# using ComboBox control
- ECCV 2022 | 机器人的交互感知与物体操作
- 技能大赛训练题:ftp 服务攻防与加固
- 推荐系统-召回阶段-2013:DSSM(双塔模型)【Embedding(语义向量)召回】【微软】
猜你喜欢

49.【拷贝构造函数与重载】

Even if the image is missing in a large area, it can also be repaired realistically. The new model CM-GAN takes into account the global structure and texture details

The operator,

【蓝桥杯选拔赛真题46】Scratch磁铁游戏 少儿编程scratch蓝桥杯选拔赛真题讲解

golang-gin-pprof-使用以及安全问题

Error IDEA Terminated with exit code 1

报错:npm ERR code EPERM

Introduction to the PartImageNet Semantic Part Segmentation dataset

For enterprises in the digital age, data governance is difficult, but it should be done

Edge Cloud Explained in Simple Depth | 4. Lifecycle Management
随机推荐
An article makes it clear!What is the difference and connection between database and data warehouse?
Node version switching management using NVM
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
DELL SC compellent 康贝存储系统怎么抓取配置信息
numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
拥塞控制,CDN,端到端
C# 中的Async 和 Await 的用法详解
CLion用于STM32开发
The operator,
Shell脚本经典案例:探测批量主机是否存活
C语言基础练(九九乘法表)与打印不同星号图案
最新完整代码:使用word2vec预训练模型进行增量训练(两种保存方式对应的两种加载方式)适用gensim各种版本
ICML2022 | 面向自监督图表示学习的全粒度自语义传播
How to quickly split and merge cell data in Excel
【蓝桥杯选拔赛真题46】Scratch磁铁游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
JSP中如何借助response对象实现页面跳转呢?
AWS implements scheduled tasks - Lambda+EventBridge
IDEA如何运行web程序
对数字化时代的企业来说,数据治理难做,但应该去做
技能大赛训练题:交换机虚拟化练习