From 9cc649f9f8461a9de0760e04db3c9b0b232e9f1e Mon Sep 17 00:00:00 2001 From: Rock Chin Date: Tue, 13 Dec 2022 13:36:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=9A=E4=B8=AAsession=E5=85=B1?= =?UTF-8?q?=E7=94=A8=E4=BA=86=E5=90=8C=E4=B8=80=E4=B8=AA=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/openai/session.py | 10 +++++++--- pkg/qqbot/manager.py | 26 ++++++++++++++++---------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/pkg/openai/session.py b/pkg/openai/session.py index 5dc537c..7a3e4ec 100644 --- a/pkg/openai/session.py +++ b/pkg/openai/session.py @@ -90,17 +90,19 @@ class Session: just_switched_to_exist_session = False - response_lock = threading.Lock() + response_lock = None # 加锁 def acquire_response_lock(self): logging.debug('{},lock acquire,{}'.format(self.name, self.response_lock)) self.response_lock.acquire() + logging.debug('{},lock acquire successfully,{}'.format(self.name, self.response_lock)) # 释放锁 def release_response_lock(self): logging.debug('{},lock release,{}'.format(self.name, self.response_lock)) self.response_lock.release() + logging.debug('{},lock release successfully,{}'.format(self.name, self.response_lock)) def __init__(self, name: str): self.name = name @@ -108,24 +110,26 @@ class Session: self.last_interact_timestamp = int(time.time()) self.schedule() + self.response_lock = threading.RLock() + # 设定检查session最后一次对话是否超过过期时间的计时器 def schedule(self): threading.Thread(target=self.expire_check_timer_loop, args=(self.create_timestamp,)).start() # 检查session是否已经过期 def expire_check_timer_loop(self, create_timestamp: int): + global sessions while True: time.sleep(60) # 不是此session已更换,退出 - if self.create_timestamp != create_timestamp: + if self.create_timestamp != create_timestamp or self not in sessions.values(): return if int(time.time()) - self.last_interact_timestamp > config.session_expire_time: logging.info('session {} 已过期'.format(self.name)) self.reset(expired=True, schedule_new=False) # 删除此session - global sessions del sessions[self.name] return diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index b16a989..9cf6505 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -86,6 +86,7 @@ class QQBotManager: try: if session_name in processing: + pkg.openai.session.get_session(session_name).release_response_lock() return "[bot]err:正在处理中,请稍后再试" processing.append(session_name) @@ -94,8 +95,9 @@ class QQBotManager: if text_message.startswith('!') or text_message.startswith("!"): # 指令 try: - logging.info("[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))] + ( - "..." if len(text_message) > 20 else ""))) + logging.info( + "[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))] + ( + "..." if len(text_message) > 20 else ""))) cmd = text_message[1:].strip().split(' ')[0] @@ -112,9 +114,10 @@ class QQBotManager: else: datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime( '%Y-%m-%d %H:%M:%S') - reply = "[bot]已切换到前一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[ - :min(100, - len(result.prompt))] + \ + reply = "[bot]已切换到前一次的对话:\n创建时间:{}\n".format( + datetime_str) + result.prompt[ + :min(100, + len(result.prompt))] + \ ("..." if len(result.prompt) > 100 else "#END#") elif cmd == 'next': result = pkg.openai.session.get_session(session_name).next_session() @@ -123,9 +126,10 @@ class QQBotManager: else: datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime( '%Y-%m-%d %H:%M:%S') - reply = "[bot]已切换到后一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[ - :min(100, - len(result.prompt))] + \ + reply = "[bot]已切换到后一次的对话:\n创建时间:{}\n".format( + datetime_str) + result.prompt[ + :min(100, + len(result.prompt))] + \ ("..." if len(result.prompt) > 100 else "#END#") elif cmd == 'prompt': reply = "[bot]当前对话所有内容:\n" + pkg.openai.session.get_session(session_name).prompt @@ -188,7 +192,8 @@ class QQBotManager: reply = "[bot]err:{}".format(e) logging.info( - "回复[{}]消息:{}".format(session_name, reply[:min(100, len(reply))] + ("..." if len(reply) > 100 else ""))) + "回复[{}]消息:{}".format(session_name, + reply[:min(100, len(reply))] + ("..." if len(reply) > 100 else ""))) reply = self.reply_filter.process(reply) finally: @@ -196,7 +201,7 @@ class QQBotManager: finally: pkg.openai.session.get_session(session_name).release_response_lock() - return reply + return reply def send(self, event, msg): asyncio.run(self.bot.send(event, msg)) @@ -265,6 +270,7 @@ class QQBotManager: # 通知系统管理员 def notify_admin(self, message: str): if hasattr(config, "admin_qq") and config.admin_qq != 0: + logging.info("通知管理员:{}".format(message)) send_task = self.bot.send_friend_message(config.admin_qq, "[bot]{}".format(message)) threading.Thread(target=asyncio.run, args=(send_task,)).start()