From 4e1be23317b4bf029c1e21bca83c9cf60609d320 Mon Sep 17 00:00:00 2001 From: InfinityPacer <160988576+InfinityPacer@users.noreply.github.com> Date: Sat, 11 May 2024 01:23:33 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E7=83=AD=E5=8A=A0=E8=BD=BD=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8F=8C=E9=87=8D=E9=98=B2=E6=8A=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/plugin.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/app/core/plugin.py b/app/core/plugin.py index 36cb47ac..f2076fb7 100644 --- a/app/core/plugin.py +++ b/app/core/plugin.py @@ -1,6 +1,7 @@ import concurrent import concurrent.futures import os +import threading import time import traceback from typing import List, Any, Dict, Tuple, Optional @@ -25,8 +26,14 @@ from app.utils.system import SystemUtils class PluginMonitorHandler(FileSystemEventHandler): + # 计时器 + __reload_timer = None + # 防抖时间间隔 + __debounce_interval = 0.5 + # 最近一次修改时间 __last_modified = 0 - __timeout = 1 + # 修改间隔 + __timeout = 2 def on_modified(self, event): """ @@ -50,8 +57,22 @@ class PluginMonitorHandler(FileSystemEventHandler): if line.startswith("class") and "(_PluginBase)" in line: pid = line.split("class ")[1].split("(_PluginBase)")[0] if pid: - logger.info(f"插件 {pid} 文件修改,重新加载...") - PluginManager().reload_plugin(pid) + # 防抖处理,通过计时器延迟加载 + if self.__reload_timer: + self.__reload_timer.cancel() + self.__reload_timer = threading.Timer(self.__debounce_interval, self.__reload_plugin, [pid]) + self.__reload_timer.start() + except Exception as e: + logger.error(f"插件文件修改后重载出错:{str(e)}") + + @staticmethod + def __reload_plugin(pid): + """ + 重新加载插件 + """ + try: + logger.info(f"插件 {pid} 文件修改,重新加载...") + PluginManager().reload_plugin(pid) except Exception as e: logger.error(f"插件文件修改后重载出错:{str(e)}")