Archived
拆掉「同步调异步 / 异步桥同步」两类垫片的第二半(HTTP 线 B2 见前序提交)。 worker 侧此前是「起线程跑同步函数、函数体内部再用 asyncio.to_thread 把活塞给 另一条循环」——线程与协程互等,取消/暂停只能靠轮询。本轮把 worker 自身变成 协程,由 _start_task 直接挂到应用事件循环上,桥接层随之失去存在理由。 改动(按子系统): - music_downloader:downloader / _aria2 / _concurrency / cover / _http / __main__ 全链路协程化。per-file 锁改 asyncio.Lock;计数闸改 「active 计数 + await asyncio.sleep(0.5)」的可取消等待(原阻塞式 with 会让 已取消任务仍要等全局名额腾出,卡死并拖后腿退款);aria2p 的同步 RPC 仍经 to_thread,轮询改 asyncio.sleep;新增 core.http_client.iter_chunks,把**流式 读取期**的 aiohttp.ClientError 收敛成 UpstreamNetworkError(请求建立阶段早已 有这条路径,读取阶段此前没有,断流会绕过调用方的 UpstreamError 分类)。 删除 get_http_session / _SESSION_LOCAL。 models._RetryFromScratch 不再继承 requests.RequestException 而继承 Exception: 它是「续传起点失效、残片已清、从头重下」,既不是确定性失败 (DownloadException),也不是残片已保留的网络失败(UpstreamError),必须独立 成类且在 except 链中先于二者捕获。 - music_api:podcast.py 全部函数协程化(直链解析内部改 await NeteaseAPI.get_song_url);compat.py 删除——16 个死掉的同步门面早已 清空,文件只剩空壳。 - services:_taskcore / _zipcore / batch / downloads / podcast / tasks 协程化。 threading.Thread → loop.create_task(任务句柄字段 worker_thread → worker_task),ThreadPoolExecutor → asyncio.gather + asyncio.Semaphore, 终态函数与结算回调全部改 await;监控线程里「worker 已死」判定改 task.done()。 删除 services/streaming.py(同步流垫片,唯一消费者已迁到 core.http_client.open)。zipfile 实例仍非线程安全,故「下载」并行、 「入包」严格串行。 - web:路由层 130+ 处 to_thread 拆除——被包住的目标自己已是协程,再包一层只是 把协程丢进线程里跑一圈。tasks_registry.start_task 直接 create_task。 web/app.py 关闭段去掉 async_bridge.shutdown()(不再有第二条约循环)。 web/routers/download.py 的播客端点补上 3 处漏掉的 await (extract_podcast_ref / get_radio_detail)——漏掉时不报错、只静默返回协程对象, 靠 pyright 抓出来后修正。 留在 to_thread 上的都是**正当的阻塞卸载**:同步 SQLite 落库与账本结算、 mutagen 全量重写标签、zipfile 整段拷贝、shutil.rmtree、aria2p 的同步 RPC、 PBKDF2 的 10 万次迭代。ruff 对残留的本地元数据操作(exists/stat/os.replace/ 往 .part 顺序写分块)按「微秒级、卸载的调度开销大于操作本身」放行,pyproject 逐条注明理由。 取消/暂停仍由 threading.Event 承载:置位方是请求线程/监控线程,读取方是 worker 协程,只有「置位」与「轮询读取」两种操作,既不需要跨循环唤醒也不会卡住事件 循环——比 asyncio.Event 更省心(ASYNC110 同理按此豁免)。 测试:10 个文件的替身边界同步迁移——网络替身从 core.sync_http 的 get_http_session 换成 downloader/服务模块的 _http.open + **真实** iter_chunks (顺带钉住读取期异常收敛);`_run` 改为在独立线程里 asyncio.run(worker(...)); aria2 的假时钟从 time.sleep 换成 _POLL_INTERVAL=0;monitor 的线程句柄替身换成 done() 可控的轻量替身(asyncio.Task 的构造需要运行中的事件循环,而监控循环跑在 常驻线程里);podcast / 封面 / 结算包装等替身一律改 async。 门禁:ruff 全过 / pyright 0-0 / pytest 383 项全绿(0 失败)。 下一步 B2:auth_system 12 个模块改 aiosqlite 背板、web 层剩余 to_thread 收尾; B3:删 core/sync_http.py、core/async_bridge.py、core/http_errors.py(现仅 sync_http 自用)。
126 lines
5.3 KiB
TOML
126 lines
5.3 KiB
TOML
# -*- coding: utf-8 -*-
|
||
[project]
|
||
name = "yunmusic-assistant"
|
||
version = "3.0.0"
|
||
description = "云村小助手 · 网易云音乐解析/下载服务(FastAPI + asyncio + aiohttp)"
|
||
requires-python = ">=3.12"
|
||
dependencies = [
|
||
"fastapi>=0.115,<1.0",
|
||
"uvicorn[standard]>=0.30,<1.0",
|
||
"aiohttp>=3.10,<4.0",
|
||
"aiosqlite>=0.20,<1.0",
|
||
"aiofiles>=24.1",
|
||
"itsdangerous>=2.2,<3.0",
|
||
"cryptography>=50.0,<51.0",
|
||
"qrcode>=7.0,<8.3",
|
||
"python-multipart>=0.0.9",
|
||
"filetype>=1.2.0",
|
||
"aria2p>=0.12.1",
|
||
"mutagen>=1.48.1",
|
||
]
|
||
|
||
[dependency-groups]
|
||
dev = [
|
||
"pytest>=8.0",
|
||
"pytest-asyncio>=0.24",
|
||
"ruff>=0.6",
|
||
"pyright>=1.1.390",
|
||
"httpx>=0.27",
|
||
"httpx2>=2.13.1",
|
||
]
|
||
|
||
# 应用工程(非库):不把自身装进环境,避免 uv 扫描顶层模块
|
||
[tool.uv]
|
||
package = false
|
||
|
||
# ============ Ruff:静态检查 + 导入排序 + 格式化 ============
|
||
[tool.ruff]
|
||
target-version = "py312"
|
||
line-length = 120
|
||
extend-exclude = [".venv", ".zcode", "templates", "static", "frontend", "data"]
|
||
|
||
[tool.ruff.lint]
|
||
# E/F 基础错误、I 导入排序、B bugbear、UP 升级、C4 推导式、ASYNC 异步陷阱、S 安全、RUF
|
||
select = ["E", "F", "W", "I", "B", "UP", "C4", "ASYNC", "S", "RUF"]
|
||
ignore = [
|
||
"E501", # 行宽交给 formatter,不强制
|
||
"S101", # 生产代码外的 assert(测试/断言脚本)
|
||
"S104", # 绑定 0.0.0.0(服务端有意为之)
|
||
"S110", # try/except/pass(本项目「失败静默降级」策略)
|
||
"S112", # try/except/continue
|
||
"S324", # hashlib.md5 用于非安全场景(上游签名口径)
|
||
"S603", "S607", # subprocess(打包/运维脚本)
|
||
"UP009", # 保留 `# -*- coding: utf-8 -*-` 文件头(全项目既有约定)
|
||
"RUF001", "RUF002", "RUF003", # 全角字符(中文注释/文案,误报)
|
||
# ---- 以下为「有意保留」,已被逐条论证为协议/设计约束,非待修缺陷 ----
|
||
"RUF012", # 常量类可变默认值:APIConstants/PodcastConstants 是常量表,非实例状态
|
||
"S305", # AES-ECB:网易云 weapi 协议**要求**的加密模式,非可自由选择
|
||
"S311", # randrange:生成上游 requestId(协议随机数),非安全用途
|
||
"ASYNC250", # input():music_api.qr_client 交互式 CLI 的人类输入,异步化无意义
|
||
"S310", # urllib:仅用于音频 Range 代理的 URL 校验路径
|
||
]
|
||
|
||
[tool.ruff.lint.per-file-ignores]
|
||
"tests/**" = ["S", "ASYNC"]
|
||
".zcode/**" = ["S", "ASYNC", "F"]
|
||
# ASYNC109:timeout 参数是有意 API——直接转发给 aiohttp 的 ClientTimeout,
|
||
# 不是「用 sleep 当超时」的反模式。
|
||
"core/http_client.py" = ["ASYNC109"]
|
||
# ASYNC109:timeout 是**总下载时限**(交给 aria2 轮询循环判定),不是「用 sleep 当超时」。
|
||
"music_downloader/_aria2.py" = ["ASYNC109"]
|
||
# ASYNC230/240:残留的阻塞文件系统调用都是**本地元数据/短写**——exists()/stat()/
|
||
# os.replace()/往 .part 顺序写分块,微秒级且不碰网络,卸载到线程的调度开销大于操作
|
||
# 本身。真正的重活(mutagen 全量重写标签、zipfile 整段拷贝、shutil.rmtree、同步
|
||
# SQLite 落库、aria2p 的同步 RPC)都已各自 to_thread 卸载。
|
||
"music_downloader/downloader.py" = ["ASYNC230", "ASYNC240"]
|
||
"services/podcast.py" = ["ASYNC230", "ASYNC240"]
|
||
"services/batch.py" = ["ASYNC240"]
|
||
# ASYNC110:暂停闸门轮询的是**跨线程**置位的 threading.Event 与任务表状态——置位方
|
||
# 在请求线程/监控线程,不属于本事件循环,故不能用 asyncio.Event 取代(见 _taskcore
|
||
# 模块 docstring 的取消/暂停载体说明)。
|
||
"services/_taskcore.py" = ["ASYNC110"]
|
||
|
||
[tool.ruff.lint.isort]
|
||
known-first-party = ["core", "web", "services", "music_api", "auth_system", "music_downloader"]
|
||
|
||
[tool.ruff.format]
|
||
quote-style = "preserve"
|
||
|
||
# ============ Pyright:类型检查 ============
|
||
[tool.pyright]
|
||
# 全量纳入(Flask 巨石删除后不再有例外清单)
|
||
# 顶层模块与 tests 此前不在名单里——「pyright 0 错误」只覆盖了 core/web/services,
|
||
# 而 requests→sync_http 迁移的遗留恰好全部躲在漏检的顶层模块中,故补齐。
|
||
include = [
|
||
"core",
|
||
"web",
|
||
"services",
|
||
"tests",
|
||
"music_api",
|
||
"auth_system",
|
||
"music_downloader",
|
||
"_mkpkg.py",
|
||
]
|
||
exclude = [".venv", ".zcode", "data", "downloads", "templates", "static", "frontend"]
|
||
venvPath = "."
|
||
venv = ".venv"
|
||
pythonVersion = "3.12"
|
||
typeCheckingMode = "standard"
|
||
reportMissingImports = true
|
||
# standard 比 basic 多出「变量可能未绑定 / 实参类型不符 / 无意义重定义 / 未使用符号」
|
||
# 等**可执行缺陷**(basic 全部漏报,实测 standard 多报 7 处真实缺陷),
|
||
# 故作为门禁级别;strict 的 reportUnknown* 需要给全部第三方库补存根,暂不启用。
|
||
# 以下三项在 standard 下已由类型系统覆盖,降为 warning 以避免第三方存根不全的噪声
|
||
reportOptionalMemberAccess = "warning"
|
||
reportAttributeAccessIssue = "warning"
|
||
|
||
# ============ Pytest ============
|
||
[tool.pytest.ini_options]
|
||
asyncio_mode = "auto"
|
||
testpaths = ["tests"]
|
||
addopts = "-q"
|
||
# 本工程不装进环境(package = false),且 tests/ 无 __init__.py,
|
||
# 故必须显式把项目根加入 import 路径——否则裸跑 `pytest` 时
|
||
# `import core` 必然 ModuleNotFoundError(只有 `python -m pytest` 才碰巧能用)。
|
||
pythonpath = ["."]
|