Refactor file conversion to use async execution

- Wrap sync conversion in async function
- Use asyncio.to_thread for non-blocking IO
- Maintain same functionality as before
- Clean up temporary files properly
- Improve responsiveness of file processing
This commit is contained in:
yangdx
2025-02-14 02:31:58 +08:00
parent cd3815e825
commit 6f999aa5e5

View File

@@ -1150,9 +1150,14 @@ def create_app(args):
pm.install("docling")
from docling.document_converter import DocumentConverter
async def convert_doc():
def sync_convert():
converter = DocumentConverter()
result = converter.convert(file_path)
content = result.document.export_to_markdown()
return result.document.export_to_markdown()
return await asyncio.to_thread(sync_convert)
content = await convert_doc()
case _:
raise ValueError(f"Unsupported file format: {ext}")
@@ -1444,9 +1449,14 @@ def create_app(args):
f.write(await file.read())
try:
async def convert_doc():
def sync_convert():
converter = DocumentConverter()
result = converter.convert(str(temp_path))
content = result.document.export_to_markdown()
return result.document.export_to_markdown()
return await asyncio.to_thread(sync_convert)
content = await convert_doc()
finally:
# Clean up the temporary file
temp_path.unlink()