python-redis-订阅和发布
发布:redishelper.py
import redis class RedisHelper: def __init__(self): self.__conn = redis.Redis(host='192.168.71.140') self.chan_sub = 'fm104.5' self.chan_pub = 'fm104.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
订阅:redis_pub.py
from redishelper import RedisHelper obi = RedisHelper()#实例化 redis_sub = obi.subscribe() while True: msg =redis_sub.parse_response() print(msg)#收到就打印收不到就卡住