Add error handling to parallel map
This commit is contained in:
parent
cbe843220a
commit
a40f456e39
7 changed files with 361 additions and 135 deletions
38
src/great_ai/utilities/parallel_map/manage_serial.py
Normal file
38
src/great_ai/utilities/parallel_map/manage_serial.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import traceback
|
||||
from typing import Any, Callable, Dict, Iterable, TypeVar
|
||||
|
||||
from tqdm.cli import tqdm
|
||||
|
||||
from ..logger import get_logger
|
||||
|
||||
logger = get_logger("parallel_map")
|
||||
|
||||
T = TypeVar("T")
|
||||
V = TypeVar("V")
|
||||
|
||||
|
||||
def manage_serial(
|
||||
*,
|
||||
function: Callable[[T], V],
|
||||
tqdm_options: Dict[str, Any],
|
||||
input_values: Iterable[T],
|
||||
ignore_exceptions: bool,
|
||||
) -> Iterable[V]:
|
||||
try:
|
||||
for v in tqdm(input_values, **tqdm_options):
|
||||
try:
|
||||
yield function(v)
|
||||
except Exception as e:
|
||||
if not ignore_exceptions:
|
||||
raise e
|
||||
else:
|
||||
logger.error(
|
||||
f"Exception {e} encountered in input, traceback:\n{traceback.format_exc()}"
|
||||
)
|
||||
except Exception as e:
|
||||
if not ignore_exceptions:
|
||||
raise e
|
||||
else:
|
||||
logger.error(
|
||||
f"Exception {e} encountered in worker, traceback:\n{traceback.format_exc()}"
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue