help:
import redis class RedisHelper(object): def __init__(self): self.__conn = redis.Redis(host='localhost') 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_pub) #调频道 pub.parse_response() #准备接收 return pub
订阅:
import redis from help import RedisHelper obj = RedisHelper() redis_sub = obj.subscribe() while True: msg = redis_sub.parse_response() print(msg)
发布:
#!/usr/bin/env python # -*- coding:utf-8 -*- from monitor.RedisHelper import RedisHelper obj = RedisHelper() obj.public('hello')