mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-22 20:36:08 +00:00
fix: 多个session共用了同一个锁
This commit is contained in:
@@ -90,17 +90,19 @@ class Session:
|
|||||||
|
|
||||||
just_switched_to_exist_session = False
|
just_switched_to_exist_session = False
|
||||||
|
|
||||||
response_lock = threading.Lock()
|
response_lock = None
|
||||||
|
|
||||||
# 加锁
|
# 加锁
|
||||||
def acquire_response_lock(self):
|
def acquire_response_lock(self):
|
||||||
logging.debug('{},lock acquire,{}'.format(self.name, self.response_lock))
|
logging.debug('{},lock acquire,{}'.format(self.name, self.response_lock))
|
||||||
self.response_lock.acquire()
|
self.response_lock.acquire()
|
||||||
|
logging.debug('{},lock acquire successfully,{}'.format(self.name, self.response_lock))
|
||||||
|
|
||||||
# 释放锁
|
# 释放锁
|
||||||
def release_response_lock(self):
|
def release_response_lock(self):
|
||||||
logging.debug('{},lock release,{}'.format(self.name, self.response_lock))
|
logging.debug('{},lock release,{}'.format(self.name, self.response_lock))
|
||||||
self.response_lock.release()
|
self.response_lock.release()
|
||||||
|
logging.debug('{},lock release successfully,{}'.format(self.name, self.response_lock))
|
||||||
|
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str):
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -108,24 +110,26 @@ class Session:
|
|||||||
self.last_interact_timestamp = int(time.time())
|
self.last_interact_timestamp = int(time.time())
|
||||||
self.schedule()
|
self.schedule()
|
||||||
|
|
||||||
|
self.response_lock = threading.RLock()
|
||||||
|
|
||||||
# 设定检查session最后一次对话是否超过过期时间的计时器
|
# 设定检查session最后一次对话是否超过过期时间的计时器
|
||||||
def schedule(self):
|
def schedule(self):
|
||||||
threading.Thread(target=self.expire_check_timer_loop, args=(self.create_timestamp,)).start()
|
threading.Thread(target=self.expire_check_timer_loop, args=(self.create_timestamp,)).start()
|
||||||
|
|
||||||
# 检查session是否已经过期
|
# 检查session是否已经过期
|
||||||
def expire_check_timer_loop(self, create_timestamp: int):
|
def expire_check_timer_loop(self, create_timestamp: int):
|
||||||
|
global sessions
|
||||||
while True:
|
while True:
|
||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
|
|
||||||
# 不是此session已更换,退出
|
# 不是此session已更换,退出
|
||||||
if self.create_timestamp != create_timestamp:
|
if self.create_timestamp != create_timestamp or self not in sessions.values():
|
||||||
return
|
return
|
||||||
if int(time.time()) - self.last_interact_timestamp > config.session_expire_time:
|
if int(time.time()) - self.last_interact_timestamp > config.session_expire_time:
|
||||||
logging.info('session {} 已过期'.format(self.name))
|
logging.info('session {} 已过期'.format(self.name))
|
||||||
self.reset(expired=True, schedule_new=False)
|
self.reset(expired=True, schedule_new=False)
|
||||||
|
|
||||||
# 删除此session
|
# 删除此session
|
||||||
global sessions
|
|
||||||
del sessions[self.name]
|
del sessions[self.name]
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
+16
-10
@@ -86,6 +86,7 @@ class QQBotManager:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if session_name in processing:
|
if session_name in processing:
|
||||||
|
pkg.openai.session.get_session(session_name).release_response_lock()
|
||||||
return "[bot]err:正在处理中,请稍后再试"
|
return "[bot]err:正在处理中,请稍后再试"
|
||||||
|
|
||||||
processing.append(session_name)
|
processing.append(session_name)
|
||||||
@@ -94,8 +95,9 @@ class QQBotManager:
|
|||||||
|
|
||||||
if text_message.startswith('!') or text_message.startswith("!"): # 指令
|
if text_message.startswith('!') or text_message.startswith("!"): # 指令
|
||||||
try:
|
try:
|
||||||
logging.info("[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))] + (
|
logging.info(
|
||||||
"..." if len(text_message) > 20 else "")))
|
"[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))] + (
|
||||||
|
"..." if len(text_message) > 20 else "")))
|
||||||
|
|
||||||
cmd = text_message[1:].strip().split(' ')[0]
|
cmd = text_message[1:].strip().split(' ')[0]
|
||||||
|
|
||||||
@@ -112,9 +114,10 @@ class QQBotManager:
|
|||||||
else:
|
else:
|
||||||
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime(
|
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime(
|
||||||
'%Y-%m-%d %H:%M:%S')
|
'%Y-%m-%d %H:%M:%S')
|
||||||
reply = "[bot]已切换到前一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[
|
reply = "[bot]已切换到前一次的对话:\n创建时间:{}\n".format(
|
||||||
:min(100,
|
datetime_str) + result.prompt[
|
||||||
len(result.prompt))] + \
|
:min(100,
|
||||||
|
len(result.prompt))] + \
|
||||||
("..." if len(result.prompt) > 100 else "#END#")
|
("..." if len(result.prompt) > 100 else "#END#")
|
||||||
elif cmd == 'next':
|
elif cmd == 'next':
|
||||||
result = pkg.openai.session.get_session(session_name).next_session()
|
result = pkg.openai.session.get_session(session_name).next_session()
|
||||||
@@ -123,9 +126,10 @@ class QQBotManager:
|
|||||||
else:
|
else:
|
||||||
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime(
|
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime(
|
||||||
'%Y-%m-%d %H:%M:%S')
|
'%Y-%m-%d %H:%M:%S')
|
||||||
reply = "[bot]已切换到后一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[
|
reply = "[bot]已切换到后一次的对话:\n创建时间:{}\n".format(
|
||||||
:min(100,
|
datetime_str) + result.prompt[
|
||||||
len(result.prompt))] + \
|
:min(100,
|
||||||
|
len(result.prompt))] + \
|
||||||
("..." if len(result.prompt) > 100 else "#END#")
|
("..." if len(result.prompt) > 100 else "#END#")
|
||||||
elif cmd == 'prompt':
|
elif cmd == 'prompt':
|
||||||
reply = "[bot]当前对话所有内容:\n" + pkg.openai.session.get_session(session_name).prompt
|
reply = "[bot]当前对话所有内容:\n" + pkg.openai.session.get_session(session_name).prompt
|
||||||
@@ -188,7 +192,8 @@ class QQBotManager:
|
|||||||
reply = "[bot]err:{}".format(e)
|
reply = "[bot]err:{}".format(e)
|
||||||
|
|
||||||
logging.info(
|
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)
|
reply = self.reply_filter.process(reply)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
@@ -196,7 +201,7 @@ class QQBotManager:
|
|||||||
finally:
|
finally:
|
||||||
pkg.openai.session.get_session(session_name).release_response_lock()
|
pkg.openai.session.get_session(session_name).release_response_lock()
|
||||||
|
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
def send(self, event, msg):
|
def send(self, event, msg):
|
||||||
asyncio.run(self.bot.send(event, msg))
|
asyncio.run(self.bot.send(event, msg))
|
||||||
@@ -265,6 +270,7 @@ class QQBotManager:
|
|||||||
# 通知系统管理员
|
# 通知系统管理员
|
||||||
def notify_admin(self, message: str):
|
def notify_admin(self, message: str):
|
||||||
if hasattr(config, "admin_qq") and config.admin_qq != 0:
|
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))
|
send_task = self.bot.send_friend_message(config.admin_qq, "[bot]{}".format(message))
|
||||||
threading.Thread(target=asyncio.run, args=(send_task,)).start()
|
threading.Thread(target=asyncio.run, args=(send_task,)).start()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user