再次优化
This commit is contained in:
13
main.py
13
main.py
@@ -27,12 +27,13 @@ class PluginReloadHandler(FileSystemEventHandler):
|
||||
继承自 watchdog.events.FileSystemEventHandler,
|
||||
监听 base_plugins 目录下的文件变化,并触发插件重载。
|
||||
"""
|
||||
def __init__(self):
|
||||
def __init__(self, loop: asyncio.AbstractEventLoop):
|
||||
"""
|
||||
初始化处理器
|
||||
|
||||
设置冷却时间,防止短时间内多次触发重载。
|
||||
设置冷却时间,并保存主事件循环的引用。
|
||||
"""
|
||||
self.loop = loop
|
||||
self.last_reload_time = 0
|
||||
self.cooldown = 1.0 # 冷却时间,防止短时间内多次重载
|
||||
|
||||
@@ -64,8 +65,8 @@ class PluginReloadHandler(FileSystemEventHandler):
|
||||
logger.info("正在重载插件...")
|
||||
|
||||
try:
|
||||
# 重新扫描并加载插件
|
||||
run_in_thread_pool(load_all_plugins)
|
||||
# 使用线程安全的方式在主事件循环中运行异步的插件加载函数
|
||||
asyncio.run_coroutine_threadsafe(run_in_thread_pool(load_all_plugins), self.loop)
|
||||
logger.success("插件重载完成")
|
||||
except Exception as e:
|
||||
logger.exception(f"重载失败: {e}")
|
||||
@@ -93,7 +94,9 @@ async def main():
|
||||
# 监控 plugins 目录
|
||||
plugin_path = os.path.join(os.path.dirname(__file__), "plugins")
|
||||
|
||||
event_handler = PluginReloadHandler()
|
||||
# 获取当前事件循环并传递给处理器
|
||||
loop = asyncio.get_running_loop()
|
||||
event_handler = PluginReloadHandler(loop)
|
||||
observer = Observer()
|
||||
|
||||
if os.path.exists(plugin_path):
|
||||
|
||||
Reference in New Issue
Block a user