-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parallel Joblib Process Entries (#3933)
Add joblib backend to process entries in parallel
- Loading branch information
Showing
3 changed files
with
175 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""This module provides utility functions for getting progress bar with joblib.""" | ||
|
||
from __future__ import annotations | ||
|
||
import contextlib | ||
import os | ||
from typing import TYPE_CHECKING, Any | ||
|
||
import joblib | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Iterator | ||
|
||
from tqdm import tqdm | ||
|
||
|
||
@contextlib.contextmanager | ||
def tqdm_joblib(tqdm_object: tqdm) -> Iterator[None]: | ||
"""Context manager to patch joblib to report into tqdm progress bar given | ||
as argument. | ||
""" | ||
|
||
class TqdmBatchCompletionCallback(joblib.parallel.BatchCompletionCallBack): | ||
def __call__(self, *args: tuple, **kwargs: dict[str, Any]) -> None: | ||
"""This will be called after each batch, to update the progress bar.""" | ||
tqdm_object.update(n=self.batch_size) | ||
return super().__call__(*args, **kwargs) | ||
|
||
old_batch_callback = joblib.parallel.BatchCompletionCallBack | ||
joblib.parallel.BatchCompletionCallBack = TqdmBatchCompletionCallback | ||
try: | ||
yield tqdm_object | ||
finally: | ||
joblib.parallel.BatchCompletionCallBack = old_batch_callback | ||
tqdm_object.close() | ||
|
||
|
||
@contextlib.contextmanager | ||
def set_python_warnings(warnings): | ||
"""Context manager to set the PYTHONWARNINGS environment variable to the | ||
given value. This is useful for preventing spam when using parallel processing. | ||
""" | ||
original_warnings = os.environ.get("PYTHONWARNINGS") | ||
os.environ["PYTHONWARNINGS"] = warnings | ||
try: | ||
yield | ||
finally: | ||
if original_warnings is None: | ||
del os.environ["PYTHONWARNINGS"] | ||
else: | ||
os.environ["PYTHONWARNINGS"] = original_warnings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters