redis
RedisHelper.py
import redis
class RedisHelper(object):
def __init__(self):
self.__conn = redis.Redis(host='localhost',port=6379)#连接Redis
self.sub_channel = 'deep_engine_receive' #定义名称
self.pub_channel = "deep_engine_send"
def publish(self, msg):#定义发布方法
try:
self.__conn.publish(self.pub_channel,msg)
return True
except Exception as e:
print(f'publish failed with {e}')
def subscribe(self):#定义订阅方法
try:
pub = self.__conn.pubsub()
pub.subscribe(self.sub_channel)
pub.parse_response()
return pub
except Exception as e:
print(f'subscribe failed with {e}')