fix async apis

This commit is contained in:
jxxghp
2025-08-01 20:27:22 +08:00
parent cc69d3b8d1
commit e32b6e07b4
7 changed files with 79 additions and 44 deletions

View File

@@ -8,6 +8,7 @@ from pathlib import Path
from typing import Optional, Any, Tuple, List, Set, Union, Dict
import aiofiles
from aiopath import AsyncPath
from qbittorrentapi import TorrentFilesList
from transmission_rpc import File
@@ -106,6 +107,18 @@ class ChainBase(metaclass=ABCMeta):
if cache_path.exists():
cache_path.unlink()
@staticmethod
async def async_remove_cache(filename: str) -> None:
"""
异步删除本地缓存
"""
cache_path = AsyncPath(settings.TEMP_PATH) / filename
if await cache_path.exists():
try:
await cache_path.unlink()
except Exception as err:
logger.error(f"异步删除缓存 {filename} 出错:{str(err)}")
@staticmethod
def __is_valid_empty(ret):
"""

View File

@@ -60,6 +60,21 @@ class TorrentsChain(ChainBase):
else:
return self.load_cache(self._rss_file) or {}
async def async_get_torrents(self, stype: Optional[str] = None) -> Dict[str, List[Context]]:
"""
异步获取当前缓存的种子
:param stype: 强制指定缓存类型spider:爬虫缓存rss:rss缓存
"""
if not stype:
stype = settings.SUBSCRIBE_MODE
# 异步读取缓存
if stype == 'spider':
return await self.async_load_cache(self._spider_file) or {}
else:
return await self.async_load_cache(self._rss_file) or {}
def clear_torrents(self):
"""
清理种子缓存数据
@@ -69,6 +84,15 @@ class TorrentsChain(ChainBase):
self.remove_cache(self._rss_file)
logger.info(f'种子缓存数据清理完成')
async def async_clear_torrents(self):
"""
异步清理种子缓存数据
"""
logger.info(f'开始异步清理种子缓存数据 ...')
await self.async_remove_cache(self._spider_file)
await self.async_remove_cache(self._rss_file)
logger.info(f'异步种子缓存数据清理完成')
def browse(self, domain: str, keyword: Optional[str] = None, cat: Optional[str] = None,
page: Optional[int] = 0) -> List[TorrentInfo]:
"""

View File

@@ -1,4 +1,3 @@
import gc
import queue
import re
import threading