From d2800ac58b794c26f532de78c02ed7066d1c88df Mon Sep 17 00:00:00 2001 From: LINSTCL Date: Sun, 5 Mar 2023 16:41:12 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0=E6=8E=A7=E5=88=B6=E7=BA=BF=E7=A8=8B=E6=95=B0=E9=87=8F?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E9=AB=98=E5=B9=B6=E5=8F=91=E5=B4=A9?= =?UTF-8?q?=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 5 +++++ main.py | 2 +- pkg/qqbot/manager.py | 25 +++++++++++++------------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/config-template.py b/config-template.py index 73f50354..898fcd9e 100644 --- a/config-template.py +++ b/config-template.py @@ -196,6 +196,11 @@ hide_exce_info_to_user = False # 设置为空字符串时,不发送提示信息 alter_tip_message = '出错了,请稍后再试' +# 机器人线程池大小 +# 该参数决定机器人可以同时处理几个人的消息,超出线程池数量的请求会被阻塞,不会被丢弃 +# 如果你不清楚该参数的意义,请不要更改 +pool_num = 10 + # 每个会话的过期时间,单位为秒 # 默认值20分钟 session_expire_time = 60 * 20 diff --git a/main.py b/main.py index 379cbfbc..8377dccb 100644 --- a/main.py +++ b/main.py @@ -180,7 +180,7 @@ def main(first_time_init=False): # 初始化qq机器人 qqbot = pkg.qqbot.manager.QQBotManager(mirai_http_api_config=config.mirai_http_api_config, timeout=config.process_message_timeout, retry=config.retry_times, - first_time_init=first_time_init) + first_time_init=first_time_init, pool_num=config.pool_num) # 加载插件 import pkg.plugin.host diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 46345398..3fb4815d 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -2,6 +2,7 @@ import asyncio import json import os import threading +from concurrent.futures import ThreadPoolExecutor import mirai.models.bus from mirai import At, GroupMessage, MessageEvent, Mirai, StrangerMessage, WebSocketAdapter, HTTPAdapter, \ @@ -20,13 +21,6 @@ import pkg.utils.context import pkg.plugin.host as plugin_host import pkg.plugin.models as plugin_models - -# 并行运行 -def go(func, args=()): - thread = threading.Thread(target=func, args=args, daemon=True) - thread.start() - - # 检查消息是否符合泛响应匹配机制 def check_response_rule(text: str): config = pkg.utils.context.get_config() @@ -55,6 +49,9 @@ def check_response_rule(text: str): class QQBotManager: retry = 3 + #线程池控制 + pool = None + bot: Mirai = None reply_filter = None @@ -64,10 +61,11 @@ class QQBotManager: ban_person = [] ban_group = [] - def __init__(self, mirai_http_api_config: dict, timeout: int = 60, retry: int = 3, first_time_init=True): - + def __init__(self, mirai_http_api_config: dict, timeout: int = 60, retry: int = 3, pool_num: int = 10, first_time_init=True): self.timeout = timeout self.retry = retry + self.pool_num = pool_num + self.pool = ThreadPoolExecutor(max_workers=self.pool_num) # 加载禁用列表 if os.path.exists("banlist.py"): @@ -116,7 +114,7 @@ class QQBotManager: self.on_person_message(event) - go(friend_message_handler, (event,)) + self.go(friend_message_handler, event) @self.bot.on(StrangerMessage) async def on_stranger_message(event: StrangerMessage): @@ -136,7 +134,7 @@ class QQBotManager: self.on_person_message(event) - go(stranger_message_handler, (event,)) + self.go(stranger_message_handler, event) @self.bot.on(GroupMessage) async def on_group_message(event: GroupMessage): @@ -156,7 +154,7 @@ class QQBotManager: self.on_group_message(event) - go(group_message_handler, (event,)) + self.go(group_message_handler, event) def unsubscribe_all(): """取消所有订阅 @@ -173,6 +171,9 @@ class QQBotManager: self.unsubscribe_all = unsubscribe_all + def go(self, func, *args, **kwargs): + self.pool.submit(func, *args, **kwargs) + def first_time_init(self, mirai_http_api_config: dict): """热重载后不再运行此函数""" From 59877bf71d8924418a25bef86a384a0cfd176664 Mon Sep 17 00:00:00 2001 From: LINSTCL Date: Sun, 5 Mar 2023 16:47:07 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/manager.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 3fb4815d..8adf9f58 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -64,8 +64,10 @@ class QQBotManager: def __init__(self, mirai_http_api_config: dict, timeout: int = 60, retry: int = 3, pool_num: int = 10, first_time_init=True): self.timeout = timeout self.retry = retry + logging.info("Register thread pool Size:{}".format(pool_num)) self.pool_num = pool_num self.pool = ThreadPoolExecutor(max_workers=self.pool_num) + logging.info('done') # 加载禁用列表 if os.path.exists("banlist.py"): From d9405d8d5db92b89e83c9442c21aee6d02257d8f Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 6 Mar 2023 08:48:50 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20main.py=E7=9A=84=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=85=BC=E5=AE=B9=E6=80=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 8377dccb..c657ea46 100644 --- a/main.py +++ b/main.py @@ -180,7 +180,8 @@ def main(first_time_init=False): # 初始化qq机器人 qqbot = pkg.qqbot.manager.QQBotManager(mirai_http_api_config=config.mirai_http_api_config, timeout=config.process_message_timeout, retry=config.retry_times, - first_time_init=first_time_init, pool_num=config.pool_num) + first_time_init=first_time_init, + pool_num=config.pool_num if hasattr(config, 'pool_num') else 10) # 加载插件 import pkg.plugin.host From 3f638adcf960c8144ada3b2f1e9d1a3f70af6b18 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 6 Mar 2023 08:50:28 +0800 Subject: [PATCH 4/4] =?UTF-8?q?perf(qqbot/manager.py):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=97=A5=E5=BF=97=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/manager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 8adf9f58..a2973d5f 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -64,10 +64,10 @@ class QQBotManager: def __init__(self, mirai_http_api_config: dict, timeout: int = 60, retry: int = 3, pool_num: int = 10, first_time_init=True): self.timeout = timeout self.retry = retry - logging.info("Register thread pool Size:{}".format(pool_num)) + self.pool_num = pool_num self.pool = ThreadPoolExecutor(max_workers=self.pool_num) - logging.info('done') + logging.debug("Registered thread pool Size:{}".format(pool_num)) # 加载禁用列表 if os.path.exists("banlist.py"):