Optimize LoRA scanning and caching with asynchronous improvements

- Implement asynchronous directory scanning with concurrent tasks
- Add fallback mechanism for uninitialized cache
- Improve error handling and logging during file processing
- Enhance file scanning with safer async recursive traversal
This commit is contained in:
Will Miao
2025-02-04 19:03:09 +08:00
parent 44306e3a8e
commit 78f16ad651
2 changed files with 54 additions and 14 deletions

View File

@@ -5,6 +5,7 @@ from .routes.lora_routes import LoraRoutes
from .routes.api_routes import ApiRoutes
from .services.lora_scanner import LoraScanner
from .services.file_monitor import LoraFileMonitor
from .services.lora_cache import LoraCache
class LoraManager:
"""Main entry point for LoRA Manager plugin"""
@@ -45,8 +46,8 @@ class LoraManager:
async def _schedule_cache_init(cls, scanner: LoraScanner):
"""Schedule cache initialization in the running event loop"""
try:
# Create the initialization task
asyncio.create_task(cls._initialize_cache(scanner))
# 创建低优先级的初始化任务
asyncio.create_task(cls._initialize_cache(scanner), name='lora_cache_init')
except Exception as e:
print(f"LoRA Manager: Error scheduling cache initialization: {e}")
@@ -54,6 +55,15 @@ class LoraManager:
async def _initialize_cache(cls, scanner: LoraScanner):
"""Initialize cache in background"""
try:
# 设置初始缓存占位
scanner._cache = LoraCache(
raw_data=[],
sorted_by_name=[],
sorted_by_date=[],
folders=[]
)
# 分阶段加载缓存
await scanner.get_cached_data(force_refresh=True)
print("LoRA Manager: Cache initialization completed")
except Exception as e: