From 61cd2bfff3cae969c0cb7f2f03daa5e96c38660b Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 2 Jan 2023 12:00:10 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=AE=8C=E5=96=84=E9=87=8D=E8=BD=BD?= =?UTF-8?q?=E5=90=8E=E7=9A=84=E5=90=AF=E5=8A=A8=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 10 +--------- pkg/qqbot/process.py | 19 +++++++------------ pkg/utils/reloader.py | 3 ++- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index b7fc6d91..0a124a57 100644 --- a/main.py +++ b/main.py @@ -87,6 +87,7 @@ def main(first_time_init=False): try: time.sleep(10000) if qqbot != pkg.utils.context.get_qqbot_manager(): # 已经reload了 + logging.info("以前的main流程由于reload退出") break except KeyboardInterrupt: stop() @@ -103,15 +104,6 @@ def stop(): qqbot_inst = pkg.utils.context.get_qqbot_manager() assert isinstance(qqbot_inst, pkg.qqbot.manager.QQBotManager) - # try: - # asyncio.run(qqbot_inst.bot.shutdown()) - # except ValueError: - # pass - # - # import mirai.utils - # MiraiRunner.__class__._instance = None - # mirai.utils.Singleton._instance = None - pkg.utils.context.get_openai_manager().key_mgr.dump_fee() for session in pkg.openai.session.sessions: logging.info('持久化session: %s', session) diff --git a/pkg/qqbot/process.py b/pkg/qqbot/process.py index 0145fcec..180bd1ac 100644 --- a/pkg/qqbot/process.py +++ b/pkg/qqbot/process.py @@ -161,21 +161,16 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes and not config.include_image_description): reply.append(" ".join(params)) elif cmd == 'reload' and launcher_type == 'person' and launcher_id == config.admin_qq: - try: - # pkg.utils.reloader.reload_all() - threading.Thread(target=pkg.utils.reloader.reload_all, daemon=True).start() - # reply = ["[bot]已重新加载所有模块"] - except Exception as e: - logging.error("reload failed:{}".format(e)) - reply = ["[bot]重载失败:{}".format(e)] + def reload_task(): + pkg.utils.reloader.reload_all() + + threading.Thread(target=reload_task, daemon=True).start() elif cmd == 'update' and launcher_type == 'person' and launcher_id == config.admin_qq: - try: + def update_task(): pkg.utils.updater.update_all() pkg.utils.reloader.reload_all() - reply = ["[bot]已更新所有模块"] - except Exception as e: - logging.error("update failed:{}".format(e)) - reply = ["[bot]更新失败:{}".format(e)] + + threading.Thread(target=update_task, daemon=True).start() except Exception as e: mgr.notify_admin("{}指令执行失败:{}".format(session_name, e)) logging.exception(e) diff --git a/pkg/utils/reloader.py b/pkg/utils/reloader.py index 1a883546..10bfff49 100644 --- a/pkg/utils/reloader.py +++ b/pkg/utils/reloader.py @@ -37,6 +37,7 @@ def reload_all(): # 执行启动流程 logging.info("执行程序启动流程") - main.main() + threading.Thread(target=main.main, args=(False,), daemon=False).start() logging.info('程序启动完成') + pkg.utils.context.get_qqbot_manager().notify_admin("重载完成")