提高协程细粒度

This commit is contained in:
xunbu
2025-07-16 12:13:24 +08:00
parent 8f68eddeb1
commit 51a33ce9b5
3 changed files with 11 additions and 6 deletions

View File

@@ -141,14 +141,14 @@ async def _perform_translation(task_id: str, params: Dict[str, Any], file_conten
refine=params['refine_markdown'], save=False
)
md_content = ft.export_to_markdown()
md_zip_content = ft.export_to_unembed_markdown()
md_zip_content = await ft.export_to_unembed_markdown_async()
try:
await httpx_client.head("https://s4.zstatic.net/ajax/libs/KaTeX/0.16.9/contrib/auto-render.min.js",
timeout=3)
html_content = ft.export_to_html(title=task_state["original_filename_stem"], cdn=True)
html_content = await ft.export_to_html_async(title=task_state["original_filename_stem"], cdn=True)
except (httpx.TimeoutException, httpx.RequestError):
task_logger.info("CDN连接失败使用本地JS进行渲染。")
html_content = ft.export_to_html(title=task_state["original_filename_stem"], cdn=False)
html_content = await ft.export_to_html_async(title=task_state["original_filename_stem"], cdn=False)
end_time = time.time()
duration = end_time - task_state["task_start_time"]

View File

@@ -120,7 +120,7 @@ class ConverterMineru(Converter):
time1 = time.time()
batch_id = await self.upload_async(document)
file_url = await self.get_file_url_async(batch_id)
result = await get_md_from_zip_url_with_inline_images_async(file_url)
result = await get_md_from_zip_url_with_inline_images_async(zip_url=file_url)
self.logger.info(f"已转换为markdown耗时{time.time() - time1}")
return result
@@ -194,7 +194,7 @@ async def get_md_from_zip_url_with_inline_images_async(
response = await client_async.get(zip_url) # 增加超时
response.raise_for_status()
print("ZIP文件下载完成。")
return await asyncio.to_thread(embed_inline_image_from_zip(response.content, filename_in_zip=filename_in_zip, encoding=encoding))
return await asyncio.to_thread(embed_inline_image_from_zip,response.content, filename_in_zip=filename_in_zip, encoding=encoding)
except httpx.HTTPStatusError as e:

View File

@@ -1,3 +1,4 @@
import asyncio
import html
import io
import logging
@@ -330,6 +331,9 @@ class FileTranslater:
self._markdown_format()
return unembed_base64_images_to_zip(self.markdown, markdown_name=filename.name)
async def export_to_unembed_markdown_async(self, filename: str | Path | None = None) -> bytes:
return await asyncio.to_thread(self.export_to_unembed_markdown,filename)
def save_as_html(self, filename: str | Path | None = None, output_dir: str | Path = "./output"):
if isinstance(filename, str):
filename = Path(filename)
@@ -403,7 +407,8 @@ class FileTranslater:
mermaid=mermaid,
)
return render
async def export_to_html_async(self, title="title", cdn=True):
return await asyncio.to_thread(self.export_to_html,title,cdn)
def translate_file(self, file_path: Path | str | None = None, to_lang="中文", output_dir="./output",
formula=True,
code=True, output_format: Literal["markdown", "html"] = "markdown", refine=False,