diff options
| author | Hunt <lizhunt@amazon.com> | 2025-06-03 11:58:25 -0700 |
|---|---|---|
| committer | Hunt <lizhunt@amazon.com> | 2025-06-03 11:58:25 -0700 |
| commit | ee9ad10b5cc9850c3e2ed1946f70e0cef429fb48 (patch) | |
| tree | b6b0411b02127951cc28292425a35a1830c2758d /dots_manager/utils.py | |
| parent | 64d060d2730cd212b2932879036eb33f7336ef38 (diff) | |
| download | dots-ee9ad10b5cc9850c3e2ed1946f70e0cef429fb48.tar.gz dots-ee9ad10b5cc9850c3e2ed1946f70e0cef429fb48.zip | |
Refactor
Diffstat (limited to 'dots_manager/utils.py')
| -rw-r--r-- | dots_manager/utils.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/dots_manager/utils.py b/dots_manager/utils.py index e8210cc..1b900f0 100644 --- a/dots_manager/utils.py +++ b/dots_manager/utils.py @@ -1,8 +1,11 @@ -from typing import Callable, Optional, TypeVar, Tuple, ParamSpec, Dict, Any +import logging +from typing import Callable, Optional, TypeVar, Tuple, ParamSpec, Dict, Any, List +from concurrent.futures import ThreadPoolExecutor, as_completed from functools import reduce P = ParamSpec("P") T = TypeVar("T") +R = TypeVar("R") def is_some( @@ -23,3 +26,19 @@ def merge_dicts(*dicts: Dict[str, Any]) -> Dict[str, Any]: return out return reduce(merge, dicts, {}) + + +def parallelize( + worker: Callable[[T], R], + items: List[T], + logger: logging.Logger, + executor: Optional[ThreadPoolExecutor] = None, +) -> List[R]: + if executor is None: + from dots_manager.config import Constants + + executor = ThreadPoolExecutor(max_workers=Constants.max_workers) + with executor as exec: + futures = [exec.submit(worker, item) for item in items] + logger.info(f"submitted {len(futures)} tasks to executor <_mood.excited>") + return [f.result() for f in as_completed(futures)] |
