基于redis的消息订阅与发布

Redis 的 SUBSCRIBE 命令可以让客户端订阅任意数量的频道, 每当有新信息发送到被订阅的频道时, 信息就会被发送给所有订阅指定频道的客户端。

作为例子, 下图展示了频道 channel1 , 以及订阅这个频道的三个客户端 —— client2 、 client5 和 client1 之间的关系:

digraph pubsub_relation {      rankdir = BT;      node [style = filled];      edge [style = bold];      channel1 [label = "channel1", fillcolor = "#A8E270"];      node [shape = box, fillcolor = "#95BBE3"];      client2 [label = "client2"];     client5 [label = "client5"];     client1 [label = "client1"];      client2 -> channel1 [label = "subscribe"];     client5 -> channel1 [label = "subscribe"];     client1 -> channel1 [label = "subscribe"]; }

当有新消息通过 PUBLISH 命令发送给频道 channel1 时, 这个消息就会被发送给订阅它的三个客户端:

digraph send_message_to_subscriber {          node [style = filled];      edge [style = "dashed, bold"];          message [label = "PUBLISH channel1 message", shape = plaintext, fillcolor = "#FADCAD"];      message -> channel1 [color = "#B22222]"];      channel1 [label = "channel1", fillcolor = "#A8E270"];      node [shape = box];      client2 [label = "client2", fillcolor = "#95BBE3"];     client5 [label = "client5", fillcolor = "#95BBE3"];     client1 [label = "client1", fillcolor = "#95BBE3"];      /*     client2 -> channel1 [label = "subscribe"];     client5 -> channel1 [label = "subscribe"];     client1 -> channel1 [label = "subscribe"];     */      channel1 -> client2 [label = "message", color = "#B22222"];     channel1 -> client5 [label = "message", color = "#B22222"];     channel1 -> client1 [label = "message", color = "#B22222"]; }

 

代码实现:

  定义一个类,实现了订阅发布的方法:

  

# -*- coding:utf-8 -*-
import redis


class SubscribePublished(object):
    '''
    用redis实现消息订阅与发布
    '''

    def __init__(self):
        # 初始化与redis的连接
        self.connect = redis.Redis(host='127.0.0.1')
        # 消息发布的通道
        self.put_channel = 'channel1'
        # 被订阅的通道
        self.sub_channel = 'channel1'

    def publish(self, message):
        # 接受消息,并发送到指定通道
        self.connect.publish(self.put_channel, message)
        return True

    def subscribe(self):
        pub = self.connect.pubsub()
        # 连接到指定通道
        pub.subscribe(self.sub_channel)
        # 接受消息
        pub.parse_response()
        return pub

 

 

  publish

# -*- coding:utf-8 -*-
from redis_test import SubscribePublished

redis_obj = SubscribePublished()

while True:
    # 模拟创建消息
    message = input('please input message:')
    # 消息发往指定通道
    redis_obj.publish(message)

  

  Subscribe
# -*- coding:utf-8 -*-

from redis_test import SubscribePublished

redis_obj = SubscribePublished()
# 客户端和要订阅的频道在 pubsub_channels 字典中关联起来
redis_sub = redis_obj.subscribe()

while True:
    # parse_response 接受消息
    message = redis_sub.parse_response()
    print(message)

 

先启动2个Subscribe,等待订阅消息:

  python3 redis_sub.py

在启动一个Published发布消息:

  python3 redis_pub.py

 

看结果:

  发布消息:

  

 

  订阅消息:

  

  

 

 

  

  

 

转载于:https://www.cnblogs.com/wangbaojun/p/11269429.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值