提高协程细粒度
This commit is contained in:
@@ -141,14 +141,14 @@ async def _perform_translation(task_id: str, params: Dict[str, Any], file_conten
|
|||||||
refine=params['refine_markdown'], save=False
|
refine=params['refine_markdown'], save=False
|
||||||
)
|
)
|
||||||
md_content = ft.export_to_markdown()
|
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:
|
try:
|
||||||
await httpx_client.head("https://s4.zstatic.net/ajax/libs/KaTeX/0.16.9/contrib/auto-render.min.js",
|
await httpx_client.head("https://s4.zstatic.net/ajax/libs/KaTeX/0.16.9/contrib/auto-render.min.js",
|
||||||
timeout=3)
|
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):
|
except (httpx.TimeoutException, httpx.RequestError):
|
||||||
task_logger.info("CDN连接失败,使用本地JS进行渲染。")
|
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()
|
end_time = time.time()
|
||||||
duration = end_time - task_state["task_start_time"]
|
duration = end_time - task_state["task_start_time"]
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ class ConverterMineru(Converter):
|
|||||||
time1 = time.time()
|
time1 = time.time()
|
||||||
batch_id = await self.upload_async(document)
|
batch_id = await self.upload_async(document)
|
||||||
file_url = await self.get_file_url_async(batch_id)
|
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}秒")
|
self.logger.info(f"已转换为markdown,耗时{time.time() - time1}秒")
|
||||||
return result
|
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 = await client_async.get(zip_url) # 增加超时
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
print("ZIP文件下载完成。")
|
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:
|
except httpx.HTTPStatusError as e:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
import html
|
import html
|
||||||
import io
|
import io
|
||||||
import logging
|
import logging
|
||||||
@@ -330,6 +331,9 @@ class FileTranslater:
|
|||||||
self._markdown_format()
|
self._markdown_format()
|
||||||
return unembed_base64_images_to_zip(self.markdown, markdown_name=filename.name)
|
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"):
|
def save_as_html(self, filename: str | Path | None = None, output_dir: str | Path = "./output"):
|
||||||
if isinstance(filename, str):
|
if isinstance(filename, str):
|
||||||
filename = Path(filename)
|
filename = Path(filename)
|
||||||
@@ -403,7 +407,8 @@ class FileTranslater:
|
|||||||
mermaid=mermaid,
|
mermaid=mermaid,
|
||||||
)
|
)
|
||||||
return render
|
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",
|
def translate_file(self, file_path: Path | str | None = None, to_lang="中文", output_dir="./output",
|
||||||
formula=True,
|
formula=True,
|
||||||
code=True, output_format: Literal["markdown", "html"] = "markdown", refine=False,
|
code=True, output_format: Literal["markdown", "html"] = "markdown", refine=False,
|
||||||
|
|||||||
Reference in New Issue
Block a user