Threaded PDF parser that decodes pages from multiple documents in parallel.
| 1391 | |
| 1392 | |
| 1393 | class DoclingThreadedPdfParser: |
| 1394 | """Threaded PDF parser that decodes pages from multiple documents in parallel.""" |
| 1395 | |
| 1396 | def __init__( |
| 1397 | self, |
| 1398 | parser_config: ThreadedPdfParserConfig | None = None, |
| 1399 | decode_config: DecodeConfig | None = None, |
| 1400 | ): |
| 1401 | if parser_config is None: |
| 1402 | parser_config = ThreadedPdfParserConfig() |
| 1403 | |
| 1404 | self._parser_config = parser_config |
| 1405 | if parser_config.render_config is not None: |
| 1406 | parser_config.render_config = _validated_render_config( |
| 1407 | parser_config.render_config |
| 1408 | ) |
| 1409 | self._decode_config = (decode_config or DecodeConfig()).model_copy() |
| 1410 | self._content_config = ( |
| 1411 | parser_config.page_content_config or ContentConfig() |
| 1412 | ).model_copy() |
| 1413 | self._batch_content_config = self._content_config.model_copy() |
| 1414 | # The threaded C++ parser decodes the whole batch with one fixed config. |
| 1415 | self._cpp_decode_config = _compile_decode_config( |
| 1416 | decode_config=self._decode_config, |
| 1417 | page_boundary=parser_config.boundary_type.value, |
| 1418 | content_config=self._batch_content_config, |
| 1419 | ) |
| 1420 | self._page_counts: Dict[str, int] = {} |
| 1421 | self._scheduled_page_counts: Dict[str, int] = {} |
| 1422 | |
| 1423 | if parser_config.render_config is None: |
| 1424 | self._parser = _threaded_pdf_parser( |
| 1425 | loglevel=parser_config.loglevel, |
| 1426 | num_threads=parser_config.threads, |
| 1427 | max_concurrent_results=parser_config.max_concurrent_results, |
| 1428 | config=self._cpp_decode_config, |
| 1429 | ) |
| 1430 | else: |
| 1431 | self._parser = _threaded_pdf_renderer( |
| 1432 | loglevel=parser_config.loglevel, |
| 1433 | num_threads=parser_config.threads, |
| 1434 | max_concurrent_results=parser_config.max_concurrent_results, |
| 1435 | decode_config=self._cpp_decode_config, |
| 1436 | render_config=parser_config.render_config, |
| 1437 | ) |
| 1438 | |
| 1439 | def load( |
| 1440 | self, |
| 1441 | path_or_stream: Union[str, Path, BytesIO], |
| 1442 | password: str | None = None, |
| 1443 | page_numbers: Sequence[int] | None = None, |
| 1444 | ) -> str: |
| 1445 | """Load a document for parallel processing. |
| 1446 | |
| 1447 | Parameters: |
| 1448 | path_or_stream: File path or BytesIO object. |
| 1449 | password: Optional password for protected files. |
| 1450 | page_numbers: Optional 1-indexed physical pages to schedule. |
no outgoing calls