mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 11:42:44 +08:00
fix: 多个session共用了同一个锁
This commit is contained in:
parent
edf7415ecf
commit
9cc649f9f8
|
@ -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
|
||||
|
||||
|
|
|
@ -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,7 +95,8 @@ class QQBotManager:
|
|||
|
||||
if text_message.startswith('!') or text_message.startswith("!"): # 指令
|
||||
try:
|
||||
logging.info("[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))] + (
|
||||
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,7 +114,8 @@ 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[
|
||||
reply = "[bot]已切换到前一次的对话:\n创建时间:{}\n".format(
|
||||
datetime_str) + result.prompt[
|
||||
:min(100,
|
||||
len(result.prompt))] + \
|
||||
("..." if len(result.prompt) > 100 else "#END#")
|
||||
|
@ -123,7 +126,8 @@ 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[
|
||||
reply = "[bot]已切换到后一次的对话:\n创建时间:{}\n".format(
|
||||
datetime_str) + result.prompt[
|
||||
:min(100,
|
||||
len(result.prompt))] + \
|
||||
("..." if len(result.prompt) > 100 else "#END#")
|
||||
|
@ -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:
|
||||
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user