发布者:
#!/usr/bin/env python# -*- coding:utf-8 -*-import redisclass RedisHelper: def __init__(self): self.__conn = redis.Redis(host='10.211.55.4') 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 pubobj = RedisHelper()obj.public('hello world')
订阅者:
#!/usr/bin/env python# -*- coding:utf-8 -*-import redisclass RedisHelper: def __init__(self): self.__conn = redis.Redis(host='10.211.55.4') 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 pubobj = RedisHelper()redis_sub = obj.subscribe()while True: msg = redis_sub.parse_response() print(msg)