解决语音助手看门狗线程泄露的问题

This commit is contained in:
binary-husky 2023-07-15 16:41:11 +08:00
parent 7148ea0596
commit afac657aaa
2 changed files with 14 additions and 3 deletions

View File

@ -23,7 +23,7 @@ class AliyunASR():
pass pass
def test_on_close(self, *args): def test_on_close(self, *args):
# print("on_close: args=>{}".format(args)) self.aliyun_service_ok = False
pass pass
def test_on_result_chg(self, message, *args): def test_on_result_chg(self, message, *args):
@ -50,7 +50,7 @@ class AliyunASR():
rad.clean_up() rad.clean_up()
temp_folder = tempfile.gettempdir() temp_folder = tempfile.gettempdir()
TOKEN, APPKEY = get_conf('ALIYUN_TOKEN', 'ALIYUN_APPKEY') TOKEN, APPKEY = get_conf('ALIYUN_TOKEN', 'ALIYUN_APPKEY')
self.aliyun_service_ok = True
URL="wss://nls-gateway.aliyuncs.com/ws/v1" URL="wss://nls-gateway.aliyuncs.com/ws/v1"
sr = nls.NlsSpeechTranscriber( sr = nls.NlsSpeechTranscriber(
url=URL, url=URL,
@ -86,4 +86,8 @@ class AliyunASR():
for i in slices: sr.send_audio(bytes(i)) for i in slices: sr.send_audio(bytes(i))
else: else:
time.sleep(0.1) time.sleep(0.1)
if not self.aliyun_service_ok:
self.stop = True
self.stop_msg = 'Aliyun音频服务异常请检查ALIYUN_TOKEN和ALIYUN_APPKEY是否过期。'
r = sr.stop() r = sr.stop()

View File

@ -14,9 +14,11 @@ class WatchDog():
self.bark_fn = bark_fn self.bark_fn = bark_fn
self.interval = interval self.interval = interval
self.msg = msg self.msg = msg
self.kill_dog = False
def watch(self): def watch(self):
while True: while True:
if self.kill_dog: break
if time.time() - self.last_feed > self.timeout: if time.time() - self.last_feed > self.timeout:
if len(self.msg) > 0: print(self.msg) if len(self.msg) > 0: print(self.msg)
self.bark_fn() self.bark_fn()
@ -87,6 +89,9 @@ class InterviewAssistant(AliyunASR):
def __del__(self): def __del__(self):
self.stop = True self.stop = True
self.stop_msg = ""
self.commit_wd.kill_dog = True
self.plugin_wd.kill_dog = True
def init(self, chatbot): def init(self, chatbot):
# 初始化音频采集线程 # 初始化音频采集线程
@ -119,7 +124,7 @@ class InterviewAssistant(AliyunASR):
self.commit_wd = WatchDog(timeout=self.commit_after_pause_n_second, bark_fn=self.no_audio_for_a_while, interval=0.2) self.commit_wd = WatchDog(timeout=self.commit_after_pause_n_second, bark_fn=self.no_audio_for_a_while, interval=0.2)
self.commit_wd.begin_watch() self.commit_wd.begin_watch()
while True: while not self.stop:
self.event_on_result_chg.wait(timeout=0.25) # run once every 0.25 second self.event_on_result_chg.wait(timeout=0.25) # run once every 0.25 second
chatbot = self.agt.update_chatbot(chatbot) # 将子线程的gpt结果写入chatbot chatbot = self.agt.update_chatbot(chatbot) # 将子线程的gpt结果写入chatbot
history = chatbot2history(chatbot) history = chatbot2history(chatbot)
@ -158,6 +163,8 @@ class InterviewAssistant(AliyunASR):
chatbot.append(["[请讲话]", "[正在等您说完问题]"]) chatbot.append(["[请讲话]", "[正在等您说完问题]"])
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
if len(self.stop_msg) != 0:
raise RuntimeError(self.stop_msg)