异步处理总结流程,防止阻塞
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
import threading
|
||||||
from typing import Dict, Any, Tuple, Optional, List
|
from typing import Dict, Any, Tuple, Optional, List
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@@ -110,9 +111,27 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
group_name = self._sanitize_group_name(group_name)
|
group_name = self._sanitize_group_name(group_name)
|
||||||
if wcf:
|
if wcf:
|
||||||
wcf.send_text("⏳群消息总结中… 😊", group_id)
|
wcf.send_text("⏳群消息总结中… 😊", group_id)
|
||||||
|
|
||||||
|
# 创建线程异步处理总结生成和发送
|
||||||
|
summary_thread = threading.Thread(
|
||||||
|
target=self._async_generate_and_send_summary,
|
||||||
|
args=(chat_content, group_name, group_id, message)
|
||||||
|
)
|
||||||
|
summary_thread.daemon = True # 设置为守护线程,主程序退出时线程也会退出
|
||||||
|
summary_thread.start()
|
||||||
|
|
||||||
|
return True, "异步总结已启动"
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.LOG.error(f"处理消息总结命令失败: {e}")
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
def _async_generate_and_send_summary(self, chat_content: str, group_name: str, group_id: str, message: Dict[str, Any]):
|
||||||
|
"""异步生成并发送总结"""
|
||||||
|
try:
|
||||||
# 生成总结
|
# 生成总结
|
||||||
summary, image_path = self._generate_summary(chat_content, group_name)
|
summary, image_path = self._generate_summary(chat_content, group_name)
|
||||||
|
|
||||||
# 发送总结结果
|
# 发送总结结果
|
||||||
wcf = message.get("wcf")
|
wcf = message.get("wcf")
|
||||||
if wcf:
|
if wcf:
|
||||||
@@ -121,12 +140,13 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
|
|
||||||
if image_path:
|
if image_path:
|
||||||
wcf.send_file(image_path, group_id)
|
wcf.send_file(image_path, group_id)
|
||||||
|
else:
|
||||||
return True, None
|
wcf.send_text("❌ 生成总结图片失败", group_id)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.LOG.error(f"处理消息总结命令失败: {e}")
|
self.LOG.error(f"异步生成总结失败: {e}")
|
||||||
return False, None
|
wcf = message.get("wcf")
|
||||||
|
if wcf:
|
||||||
|
wcf.send_text(f"❌ 生成总结失败: {str(e)}", group_id)
|
||||||
|
|
||||||
def _sanitize_group_name(self, group_name: str) -> str:
|
def _sanitize_group_name(self, group_name: str) -> str:
|
||||||
"""处理群名,去除特殊字符并限制长度"""
|
"""处理群名,去除特殊字符并限制长度"""
|
||||||
|
|||||||
Reference in New Issue
Block a user