Parallel Loader¤
The ParallelLoader class is an implementation of the Loader class optimized for large data sets using Python's ThreadPoolExecutor for parallel I/O operations.
VALID_FILE_TYPES = {*TEXT_TYPES, *PDF_TYPES, *DOCX_TYPES, *ZIP_TYPES}
module-attribute
¤
_sanitize_zip_filename(filename: str) -> str
¤
Sanitize ZIP entry names to prevent path traversal.
Source code in lexos/io/parallel_loader.py
class ParallelLoader¤
Note
Mkdocstrings does not properly render the the class docstrings because griffe_pydantic gets confused when trying to render fields inherited from BaseLoader combined with new fields in ParallelLoader. Attributes are still documented correctly, but the overall class docstring is missing.
max_workers: Optional[int] = None
pydantic-field
¤
Maximum number of worker threads. Can be an integer or will be auto-calculated based on worker_strategy.
worker_strategy: str = 'auto'
pydantic-field
¤
Worker allocation strategy: 'auto' (analyze files), 'io_bound' (more workers), 'cpu_bound' (fewer workers), 'balanced' (middle ground).
batch_size: int = 100
pydantic-field
¤
Number of files to process in each batch for memory management.
show_progress: bool = True
pydantic-field
¤
Whether to show a progress bar during loading.
callback: Optional[Callable[..., None]] = None
pydantic-field
¤
Optional callback function for custom progress handling.
__init__(**data)
¤
_get_mime_type(path: Path | str, file_start: bytes) -> str
¤
Get the mime type of a file with caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
file_start
|
bytes
|
The first bytes of the file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The mime type of the file. |
Source code in lexos/io/parallel_loader.py
_load_docx_file(path: Path | str) -> tuple[str, str, str, str, Optional[Exception]]
¤
Load a docx file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[str, str, str, str, Optional[Exception]]
|
(path_name, name, mime_type, text, error) |
Source code in lexos/io/parallel_loader.py
_load_pdf_file(path: Path | str) -> list[tuple[str, str, str, str, Optional[Exception]]]
¤
Load a pdf file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, str, str, Optional[Exception]]]
|
list[tuple]: List of (path_name, name, mime_type, text, error) for each page. |
Source code in lexos/io/parallel_loader.py
_load_text_file(path: Path | str, mime_type: str) -> tuple[str, str, str, str, Optional[Exception]]
¤
Load a text file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
mime_type
|
str
|
The mime type of the file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[str, str, str, str, Optional[Exception]]
|
(path_name, name, mime_type, text, error) |
Source code in lexos/io/parallel_loader.py
_load_zip_file(path: Path | str) -> list[tuple[str, str, str, str, Optional[Exception]]]
¤
Handle a zip file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, str, str, Optional[Exception]]]
|
list[tuple]: List of (path_name, name, mime_type, text, error) for each file in zip. |
Source code in lexos/io/parallel_loader.py
_load_file_concurrent(path: Path | str, mime_type: str) -> list[tuple[str, str, str, str, Optional[Exception]]]
¤
Load a single file (wrapper for concurrent execution).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The path to the file. |
required |
mime_type
|
str
|
The mime type of the file. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, str, str, Optional[Exception]]]
|
list[tuple]: List of (path_name, name, mime_type, text, error) tuples. |
Source code in lexos/io/parallel_loader.py
_process_results(results: list[tuple]) -> None
¤
Process and store results in a thread-safe manner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
list[tuple]
|
List of (path_name, name, mime_type, text, error) tuples. |
required |
Source code in lexos/io/parallel_loader.py
_prepare_file_list(paths: list[Path | str]) -> list[tuple[Path | str, str]]
¤
Prepare list of files with MIME types, expanding directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
list[Path | str]
|
List of file or directory paths. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[Path | str, str]]
|
list[tuple]: List of (path, mime_type) tuples. |
Source code in lexos/io/parallel_loader.py
_detect_mime_types_parallel(file_list: list[tuple[Path | str, Optional[str]]], progress: Optional[Progress] = None, task_id: Optional[int] = None) -> list[tuple[Path | str, str]]
¤
Detect MIME types for all files in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list
|
list[tuple]
|
List of (path, mime_type) tuples. |
required |
progress
|
Optional[Progress]
|
Rich progress bar instance. |
None
|
task_id
|
Optional[int]
|
Task ID for progress tracking. |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[Path | str, str]]
|
list[tuple]: List of (path, mime_type) tuples with detected types. |
Source code in lexos/io/parallel_loader.py
_group_by_type(file_list: list[tuple[Path | str, str]]) -> dict[str, list[Path | str]]
¤
Group files by MIME type for optimized processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list
|
list[tuple]
|
List of (path, mime_type) tuples. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, list[Path | str]]
|
Dictionary mapping mime_type to list of paths. |
Source code in lexos/io/parallel_loader.py
_calculate_optimal_workers(file_list: list[tuple[Path | str, str]]) -> int
¤
Calculate optimal worker count based on file types and strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list
|
list[tuple]
|
List of (path, mime_type) tuples. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Optimal number of workers. |
Source code in lexos/io/parallel_loader.py
_sort_files_by_type(file_list: list[tuple[Path | str, str]]) -> list[tuple[Path | str, str]]
¤
Sort files by MIME type for better cache locality.
Groups similar file types together to improve processing efficiency through better cache utilization and reduced context switching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list
|
list[tuple]
|
List of (path, mime_type) tuples. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[Path | str, str]]
|
list[tuple]: Sorted list of (path, mime_type) tuples. |
Source code in lexos/io/parallel_loader.py
_maybe_update_progress(progress, load_task)
¤
_maybe_call_callback(path: str, processed: int, total_files: int) -> None
¤
_process_streaming_future(future, future_to_path: dict, result_queue: Queue, progress, load_task, total_files: int, processed: int) -> int
¤
Source code in lexos/io/parallel_loader.py
_stream_results(result_queue: Queue, progress, worker_thread)
¤
Source code in lexos/io/parallel_loader.py
load(paths: Path | str | list[Path | str]) -> None
¤
Load files in parallel with batching and progress tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
Path | str | list[Path | str]
|
The list of paths to load. |
required |
Source code in lexos/io/parallel_loader.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | |
load_dataset(dataset: Self) -> None
¤
Load a dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
DataLoader
|
The dataset to load. |
required |
As of v2.10.5, Pydantic does not support recursive types (Self).
As a result, this method performs its own check to see if the
value of dataset is of type DataLoader.
Source code in lexos/io/parallel_loader.py
load_streaming(paths: Path | str | list[Path | str]) -> Generator[tuple[str, str, str, str, Optional[Exception]], None, None]
¤
Load files in parallel and yield results as they become available.
This method yields documents as they're loaded, making it suitable for streaming large datasets directly into a corpus or other consumer without holding all files in memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
Path | str | list[Path | str]
|
The list of paths to load. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[str, str, str, str, Optional[Exception]]
|
(path, name, mime_type, text, error) for each loaded document. - path (str): The file path or full path for files in archives - name (str): The document name (stem of filename) - mime_type (str): The detected MIME type - text (str): The document text content (empty string if error) - error (Optional[Exception]): Any error that occurred during loading |
Source code in lexos/io/parallel_loader.py
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | |