Redis 實現廣播訂閱
阿新 • • 發佈:2018-03-08
brush sub host edi helper rom ret parse self
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‘)
Redis 實現廣播訂閱