From cd942cfc311eb6e6099bc4f4c7a04b114463c3b3 Mon Sep 17 00:00:00 2001 From: xunbu Date: Thu, 29 May 2025 16:02:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86cdn=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E8=B6=85=E6=97=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docutranslate/app.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docutranslate/app.py b/docutranslate/app.py index 6d761b6..ca36819 100644 --- a/docutranslate/app.py +++ b/docutranslate/app.py @@ -18,6 +18,7 @@ from docutranslate.logger import translater_logger from docutranslate.utils.resource_utils import resource_path from docutranslate.global_values import available_packages +httpx_client = httpx.AsyncClient() # --- 全局配置 --- log_queue: Optional[asyncio.Queue] = None current_state: Dict[str, Any] = { @@ -134,10 +135,11 @@ async def _perform_translation(params: Dict[str, Any], file_contents: bytes, ori md_content = ft.export_to_markdown() try: - httpx.head("https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/contrib/auto-render.min.js", timeout=1) + await httpx_client.head("https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/contrib/auto-render.min.js", timeout=3) html_content = ft.export_to_html(title=current_state["original_filename_stem"], cdn=True) - except TimeoutError: - translater_logger.info("无法连接cdn,使用本地js进行pdf渲染") + except (httpx.TimeoutException, httpx.RequestError) as e: + print(f"连接cdn.jsdelivr.net失败,错误信息:{e}") + translater_logger.info("无法连接cdn.jsdelivr.net,使用本地js进行pdf渲染") html_content = ft.export_to_html(title=current_state["original_filename_stem"], cdn=False) end_time = time.time() duration = end_time - current_state["task_start_time"] @@ -223,7 +225,7 @@ async def handle_translate( content={"task_started": False, "message": "另一个翻译任务正在进行中,请稍后再试。"} ) - #可选的格式认证,这部分交给前端来写了 + # 可选的格式认证,这部分交给前端来写了 # if not file or not file.filename: # return JSONResponse( # status_code=400,