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
ยค
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)
ยค
_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
_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
_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
_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
_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
_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
_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
_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
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
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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 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 | |
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
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 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 | |