From 68cba4447602b4f3be0e2951d4f284e6a1e19146 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sun, 16 Feb 2025 17:24:17 +0800 Subject: [PATCH] fix modules load --- app/core/workflow.py | 13 ++++++++----- app/helper/module.py | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/core/workflow.py b/app/core/workflow.py index 38267b40..67237a77 100644 --- a/app/core/workflow.py +++ b/app/core/workflow.py @@ -21,19 +21,22 @@ class WorkFlowManager(metaclass=Singleton): """ 初始化 """ - def check_module(module: Any): + + def filter_func(obj: Any): """ - 检查模块 + 过滤函数,确保只加载新定义的类 """ - if not hasattr(module, 'execute') or not hasattr(module, "name"): + if not isinstance(obj, type): return False - return True + if not hasattr(obj, 'execute') or not hasattr(obj, "name"): + return False + return obj.__module__.startswith("app.actions") # 加载所有动作 self._actions = {} actions = ModuleHelper.load( "app.actions", - filter_func=lambda _, obj: check_module(obj) + filter_func=lambda _, obj: filter_func(obj) ) for action in actions: logger.debug(f"加载动作: {action.__name__}") diff --git a/app/helper/module.py b/app/helper/module.py index cc28f448..c4f05177 100644 --- a/app/helper/module.py +++ b/app/helper/module.py @@ -23,6 +23,7 @@ class ModuleHelper: """ submodules: list = [] + loaded_modules = set() packages = importlib.import_module(package_path) for importer, package_name, _ in pkgutil.iter_modules(packages.__path__): try: @@ -35,6 +36,9 @@ class ModuleHelper: if name.startswith('_'): continue if isinstance(obj, type) and filter_func(name, obj): + if name in loaded_modules: + continue + loaded_modules.add(name) submodules.append(obj) except Exception as err: logger.debug(f'加载模块 {package_name} 失败:{str(err)} - {traceback.format_exc()}')