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')