mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 12:05:54 +00:00
perf: 优化线程锁相关操作
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import openai
|
||||
|
||||
import config
|
||||
|
||||
inst = None
|
||||
|
||||
|
||||
@@ -22,6 +24,7 @@ class OpenAIInteract:
|
||||
response = openai.Completion.create(
|
||||
prompt=prompt,
|
||||
stop=stop,
|
||||
timeout=config.process_message_timeout,
|
||||
**self.api_params
|
||||
)
|
||||
return response
|
||||
|
||||
@@ -100,9 +100,10 @@ class Session:
|
||||
|
||||
# 释放锁
|
||||
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))
|
||||
if self.response_lock.locked():
|
||||
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
|
||||
@@ -110,7 +111,7 @@ class Session:
|
||||
self.last_interact_timestamp = int(time.time())
|
||||
self.schedule()
|
||||
|
||||
self.response_lock = threading.RLock()
|
||||
self.response_lock = threading.Lock()
|
||||
|
||||
# 设定检查session最后一次对话是否超过过期时间的计时器
|
||||
def schedule(self):
|
||||
@@ -144,7 +145,8 @@ class Session:
|
||||
# 向API请求补全
|
||||
response = pkg.openai.manager.get_inst().request_completion(self.cut_out(self.prompt + self.user_name + ':' +
|
||||
text + '\n' + self.bot_name + ':',
|
||||
max_rounds, max_length), self.user_name + ':')
|
||||
max_rounds, max_length),
|
||||
self.user_name + ':')
|
||||
|
||||
self.prompt += self.user_name + ':' + text + '\n' + self.bot_name + ':'
|
||||
# print(response)
|
||||
|
||||
@@ -225,10 +225,12 @@ class QQBotManager:
|
||||
reply = self.process_message('person', event.sender.id, str(event.message_chain))
|
||||
break
|
||||
except FunctionTimedOut:
|
||||
pkg.openai.session.get_session('person_{}'.format(event.sender.id)).release_response_lock()
|
||||
failed += 1
|
||||
continue
|
||||
|
||||
if failed == self.retry:
|
||||
pkg.openai.session.get_session('person_{}'.format(event.sender.id)).release_response_lock()
|
||||
self.notify_admin("{} 请求超时".format("person_{}".format(event.sender.id)))
|
||||
reply = "[bot]err:请求超时"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user