perf: 翻译独立后台循环(1 篇/秒)+ Semaphore 1

之前 fetch_one_source 入库后立即调翻译(可能并发触发腾讯 TMT 限速)
改为独立 translation_loop 后台循环:
- 完全不和 RSS 抓取并行
- 1 篇/秒节拍(Semaphore 1 + sleep 1.0)
- 没活时空闲 5 秒再轮询
- pending/failed 都重试
This commit is contained in:
Mavis
2026-06-08 00:27:09 +08:00
parent e79cfaa5f7
commit 9862a92423
6 changed files with 203 additions and 39 deletions

View File

@@ -17,7 +17,7 @@ from sqlalchemy import select
from app.config import settings
from app.database import AsyncSessionLocal
from app.models.source import Source
from app.workers.pipeline import fetch_one_source, run_once
from app.workers.pipeline import fetch_one_source, run_once, translation_loop
logger = logging.getLogger("news.worker")
logging.basicConfig(
@@ -89,6 +89,10 @@ async def main() -> None:
scheduler.start()
logger.info("scheduler started with %d jobs", len(scheduler.get_jobs()))
# 独立的翻译后台循环(不和 RSS 抓取并行;1 篇/秒)
translation_task = asyncio.create_task(translation_loop(), name="translation_loop")
logger.info("translation_loop task scheduled (1 article/sec)")
stop = asyncio.Event()
def _signal_handler():
@@ -104,7 +108,12 @@ async def main() -> None:
pass
await stop.wait()
logger.info("stopping scheduler")
logger.info("stopping scheduler and translation loop")
translation_task.cancel()
try:
await translation_task
except asyncio.CancelledError:
pass
scheduler.shutdown(wait=False)