diff --git a/app/chain/torrents.py b/app/chain/torrents.py index b7606d28..f0d59295 100644 --- a/app/chain/torrents.py +++ b/app/chain/torrents.py @@ -1,7 +1,7 @@ +import gc import re import traceback from typing import Dict, List, Union, Optional -import gc from cachetools import cached, TTLCache @@ -12,10 +12,10 @@ from app.core.context import TorrentInfo, Context, MediaInfo from app.core.metainfo import MetaInfo from app.db.site_oper import SiteOper from app.db.systemconfig_oper import SystemConfigOper +from app.helper.memory import memory_optimized, clear_large_objects from app.helper.rss import RssHelper from app.helper.sites import SitesHelper from app.helper.torrent import TorrentHelper -from app.helper.memory import MemoryManager, memory_optimized, clear_large_objects from app.log import logger from app.schemas import Notification from app.schemas.types import SystemConfigKey, MessageChannel, NotificationType, MediaType @@ -39,11 +39,6 @@ class TorrentsChain(ChainBase, metaclass=Singleton): self.systemconfig = SystemConfigOper() self.mediachain = MediaChain() self.torrenthelper = TorrentHelper() - # 初始化内存管理器 - self.memory_manager = MemoryManager() - # 设置内存阈值和启动监控 - self.memory_manager.set_threshold(settings.CACHE_CONF['memory']) - self.memory_manager.start_monitoring() def __del__(self): """ @@ -93,8 +88,6 @@ class TorrentsChain(ChainBase, metaclass=Singleton): logger.info(f'开始清理种子缓存数据 ...') self.remove_cache(self._spider_file) self.remove_cache(self._rss_file) - # 强制垃圾回收 - self.memory_manager.force_gc() logger.info(f'种子缓存数据清理完成') @cached(cache=TTLCache(maxsize=64, ttl=300)) @@ -189,8 +182,6 @@ class TorrentsChain(ChainBase, metaclass=Singleton): indexers = self.siteshelper.get_indexers() # 需要刷新的站点domain domains = [] - # 处理计数器,用于定期内存检查 - processed_count = 0 # 遍历站点缓存资源 for indexer in indexers: @@ -256,15 +247,8 @@ class TorrentsChain(ChainBase, metaclass=Singleton): torrents_cache[domain] = torrents_cache[domain][-settings.CACHE_CONF["torrents"]:] # 清理旧对象 clear_large_objects(*old_contexts) - # 优化:清理不再需要的临时变量 del meta, mediainfo, context - - # 每处理一定数量的种子后检查内存 - processed_count += 1 - if processed_count % 10 == 0: - self.memory_manager.check_memory_and_cleanup() - # 回收资源 del torrents # 定期执行垃圾回收 diff --git a/app/startup/lifecycle.py b/app/startup/lifecycle.py index 32dd0de5..a3ae1d2b 100644 --- a/app/startup/lifecycle.py +++ b/app/startup/lifecycle.py @@ -4,8 +4,8 @@ from contextlib import asynccontextmanager from fastapi import FastAPI from app.core.config import global_vars -from app.chain.torrents import TorrentsChain from app.startup.command_initializer import init_command, stop_command, restart_command +from app.startup.memory_initializer import init_memory_manager, stop_memory_manager from app.startup.modules_initializer import init_modules, stop_modules from app.startup.monitor_initializer import stop_monitor, init_monitor from app.startup.plugins_initializer import init_plugins, stop_plugins, sync_plugins @@ -35,8 +35,6 @@ async def lifespan(app: FastAPI): init_modules() # 初始化路由 init_routers(app) - # 资源缓存 - TorrentsChain() # 初始化插件 init_plugins() # 初始化定时器 @@ -47,6 +45,8 @@ async def lifespan(app: FastAPI): init_command() # 初始化工作流 init_workflow() + # 初始化内存管理 + init_memory_manager() # 插件同步到本地 sync_plugins_task = asyncio.create_task(init_plugin_system()) try: @@ -56,6 +56,7 @@ async def lifespan(app: FastAPI): print("Shutting down...") # 停止信号 global_vars.stop_system() + # 取消同步插件任务 try: sync_plugins_task.cancel() await sync_plugins_task @@ -63,6 +64,8 @@ async def lifespan(app: FastAPI): pass except Exception as e: print(str(e)) + # 停止内存管理器 + stop_memory_manager() # 停止工作流 stop_workflow() # 停止命令 diff --git a/app/startup/memory_initializer.py b/app/startup/memory_initializer.py new file mode 100644 index 00000000..d0d8cde4 --- /dev/null +++ b/app/startup/memory_initializer.py @@ -0,0 +1,19 @@ +from app.core.config import settings +from app.helper.memory import MemoryManager + + +def init_memory_manager(): + """ + 初始化内存监控器 + """ + memory_manager = MemoryManager() + # 设置内存阈值和启动监控 + memory_manager.set_threshold(settings.CACHE_CONF['memory']) + memory_manager.start_monitoring() + + +def stop_memory_manager(): + """ + 停止内存监控器 + """ + MemoryManager().stop_monitoring()