fix: 消息订阅redis连接未关闭

This commit is contained in:
xinwen
2022-02-07 17:46:53 +08:00
committed by Jiangjie.Bai
parent 54b2360843
commit 2869338e2c
2 changed files with 20 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ class RedisPubSub:
def __init__(self, ch, db=10):
self.ch = ch
self.redis = get_redis_client(db)
self.subscriber = None
def subscribe(self):
ps = self.redis.pubsub()
@@ -41,7 +42,9 @@ class RedisPubSub:
:param handle: lambda item: do_something
:return:
"""
self.close_handle_msg()
sub = self.subscribe()
self.subscriber = sub
msgs = sub.listen()
try:
@@ -65,3 +68,8 @@ class RedisPubSub:
except Exception as e:
logger.error("Redis observer close error: ", e)
def close_handle_msg(self):
if self.subscriber:
self.subscriber.close()
self.subscriber = None