Redis 实现广播订阅


RedisHelper

import redis


class RedisHelper:
    def __init__(self):
        self.__conn = redis.Redis(host='47.94.18.xxx')
        self.chan_sub = '104.5'  # 接收频道
        self.chan_pub = '104.5'  # 发送频道

    def public(self, msg):
        self.__conn.publish(self.chan_pub, msg)
        return True

    def subscribe(self):
        pub = self.__conn.pubsub()
        pub.subscribe(self.chan_sub)
        pub.parse_response()
        return pub

订阅者:

from redis_test.rds_helper import RedisHelper

obj = RedisHelper()
redis_sub = obj.subscribe()

while True:
    msg = redis_sub.parse_response()
    print(msg)

发布者:

from redis_test.rds_helper import RedisHelper

obj = RedisHelper()
obj.public('hello')

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM