Archived
chore(async-infra): 异步基础层 + 工具链 + 清理
异步基础层(core/):
- db.py aiosqlite + WAL:读路径无锁并发、写路径 asyncio.Lock 串行,
busy_timeout 自旋——实现「读写同时进行」(旧实现用 RLock 全读串行)
- http_client.py aiohttp 连接池 + 有界重试(5xx 重试 / 4xx 不重试)+
响应体上限 + 流式接口;按事件循环隔离 session(支持迁移期双 loop)
工具链:
- pyproject.toml ruff(E/F/W/I/B/UP/C4/ASYNC/S/RUF) + pyright(basic) + pytest(asyncio auto)
- requirements-fastapi.txt FastAPI/aiohttp/aiosqlite/aiofiles 等目标栈
- requirements-dev.txt 开发依赖(pytest/ruff/pyright)
清理:删除无用脚本 _csscheck.py 与产物标记 _css_ok.txt/_pkg_ok.txt,同步 .gitignore
验证:pytest 10/10(含「写 200 条同时 4 读协程零异常」并发用例);
ruff All checks passed;pyright 0 errors。
This commit is contained in:
1 parent
41356e89c4
commit
9f56f68b66
16 files changed
+706
-42
No files matched your search
+6
-5
@@ -4,10 +4,10 @@
|
||||
自 main.py 原样迁出:安全整数解析、控制台输出兜底、SRT 时间轴、
|
||||
LRC→SRT 转换、字段参数解析、163cn.tv 短链展开(含私有缓存)。
|
||||
"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
import threading
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
|
||||
def safe_int(value, default: int) -> int:
|
||||
@@ -34,7 +34,7 @@ def srt_time(t: float) -> str:
|
||||
h = int(t // 3600)
|
||||
m = int((t % 3600) // 60)
|
||||
s = int(t % 60)
|
||||
ms = int(round((t - int(t)) * 1000))
|
||||
ms = round((t - int(t)) * 1000)
|
||||
if ms >= 1000:
|
||||
ms = 999
|
||||
return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
|
||||
@@ -73,14 +73,14 @@ def format_lyric(lrc: str, lyric_format: str) -> str:
|
||||
for i, (t, text) in enumerate(timed):
|
||||
end = timed[i + 1][0] if i + 1 < len(timed) else t + 5
|
||||
if end <= t:
|
||||
end = t + 1 # 同时间多行:给 1 秒最小区间,不再生成倒挂区间
|
||||
end = t + 1 # 同时间多行:给 1 秒最小区间,不再生成倒挂区间
|
||||
out.append(f"{i + 1}\n{srt_time(t)} --> {srt_time(end)}\n{text}\n")
|
||||
if untimed:
|
||||
out.append('\n'.join(untimed))
|
||||
return '\n'.join(out)
|
||||
|
||||
|
||||
def parse_meta_fields(raw) -> Optional[List[str]]:
|
||||
def parse_meta_fields(raw) -> list[str] | None:
|
||||
"""解析写入字段参数,空返回 None(表示全部写入)"""
|
||||
if raw is None or raw == '':
|
||||
return None
|
||||
@@ -93,7 +93,7 @@ def parse_meta_fields(raw) -> Optional[List[str]]:
|
||||
|
||||
|
||||
# 短链展开结果缓存:用户反复解析同一分享链接不再重复请求 163cn.tv(有界防膨胀)
|
||||
_SHORTLINK_CACHE: Dict[str, str] = {}
|
||||
_SHORTLINK_CACHE: dict[str, str] = {}
|
||||
_SHORTLINK_CACHE_LOCK = threading.Lock()
|
||||
|
||||
|
||||
@@ -106,6 +106,7 @@ def expand_shortlink(url: str) -> str:
|
||||
expanded = url
|
||||
try:
|
||||
import requests
|
||||
|
||||
response = requests.get(url, allow_redirects=False, timeout=10)
|
||||
expanded = response.headers.get('Location', url)
|
||||
except Exception:
|
||||
|
||||
Reference in new issue
Block a user