Skip to content

Commit c34ccc1

Browse files
committed
fix(extract-python): avoid blocking the io loop when processing document
1 parent 503cad6 commit c34ccc1

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

‎extract-python/extract_python/docling_.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import shutil
23
import tempfile
34
from collections.abc import AsyncGenerator, Iterable, Iterator
@@ -55,7 +56,13 @@ async def extract_content(
5556
) -> AsyncGenerator[Result, None]:
5657
docs, path_or_streams = map_and_preserve(_to_docling, docs)
5758
outputs = self._converter.convert_all(path_or_streams, raises_on_error=False)
58-
for doc, res in zip(docs, outputs, strict=True):
59+
60+
sentinel = object()
61+
while True:
62+
res = await asyncio.to_thread(next, outputs, sentinel)
63+
if res is sentinel:
64+
return
65+
doc = next(docs)
5966
yield _to_result(res, doc, output_format, output_path=output_path)
6067

6168
@classmethod

‎extract-python/extract_python/marker_.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import gc
23
from collections.abc import AsyncGenerator, Iterable
34
from copy import deepcopy
@@ -86,23 +87,23 @@ async def extract_content(
8687
renderer=renderer,
8788
)
8889
for doc in docs:
89-
yield _process_doc(doc, converter, output_format, output_path)
90+
yield await _process_doc(doc, converter, output_format, output_path)
9091

9192
@classmethod
9293
def _from_config(cls, config: MarkerPipelineConfig) -> Self:
9394
return cls(config.config)
9495

9596

9697
@report_recoverable_errors(_MARKER_CONVERSION_ERRORS)
97-
def _process_doc(
98+
async def _process_doc(
9899
doc: InputDoc,
99100
converter: "PdfConverter",
100101
output_format: OutputFormat,
101102
output_path: Path,
102103
) -> Result:
103104
from marker.output import text_from_rendered # noqa: PLC0415
104105

105-
rendered = converter(str(doc.path))
106+
rendered = await asyncio.to_thread(converter, str(doc.path))
106107
content, _, images = text_from_rendered(rendered)
107108
match output_format:
108109
case OutputFormat.MARKDOWN:

0 commit comments

Comments
 (0)