From 6f999aa5e5c9b317899bf333b2264b10246e5bd4 Mon Sep 17 00:00:00 2001 From: yangdx Date: Fri, 14 Feb 2025 02:31:58 +0800 Subject: [PATCH] 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 --- lightrag/api/lightrag_server.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index ba0a95cc..6bdcd9b1 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -1150,9 +1150,14 @@ def create_app(args): pm.install("docling") from docling.document_converter import DocumentConverter - converter = DocumentConverter() - result = converter.convert(file_path) - content = result.document.export_to_markdown() + async def convert_doc(): + def sync_convert(): + converter = DocumentConverter() + result = converter.convert(file_path) + 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: - converter = DocumentConverter() - result = converter.convert(str(temp_path)) - content = result.document.export_to_markdown() + async def convert_doc(): + def sync_convert(): + converter = DocumentConverter() + result = converter.convert(str(temp_path)) + 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()