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:
parent
cd3815e825
commit
6f999aa5e5
1 changed files with 16 additions and 6 deletions
|
|
@ -1150,9 +1150,14 @@ def create_app(args):
|
||||||
pm.install("docling")
|
pm.install("docling")
|
||||||
from docling.document_converter import DocumentConverter
|
from docling.document_converter import DocumentConverter
|
||||||
|
|
||||||
|
async def convert_doc():
|
||||||
|
def sync_convert():
|
||||||
converter = DocumentConverter()
|
converter = DocumentConverter()
|
||||||
result = converter.convert(file_path)
|
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 _:
|
case _:
|
||||||
raise ValueError(f"Unsupported file format: {ext}")
|
raise ValueError(f"Unsupported file format: {ext}")
|
||||||
|
|
@ -1444,9 +1449,14 @@ def create_app(args):
|
||||||
f.write(await file.read())
|
f.write(await file.read())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
async def convert_doc():
|
||||||
|
def sync_convert():
|
||||||
converter = DocumentConverter()
|
converter = DocumentConverter()
|
||||||
result = converter.convert(str(temp_path))
|
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:
|
finally:
|
||||||
# Clean up the temporary file
|
# Clean up the temporary file
|
||||||
temp_path.unlink()
|
temp_path.unlink()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue