Add simpler parallel_map

This commit is contained in:
Andras Schmelczer 2022-07-09 21:33:14 +02:00
parent 7db1503a45
commit 286ede5fd3
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
3 changed files with 37 additions and 1 deletions

View file

@ -0,0 +1,30 @@
from typing import Awaitable, Callable, List, Optional, Sequence, TypeVar, Union
from tqdm.cli import tqdm
from .parallel_map import parallel_map
T = TypeVar("T")
V = TypeVar("V")
def simple_parallel_map(
func: Callable[[T], Union[V, Awaitable[V]]],
input_values: Sequence[T],
*,
chunk_size: Optional[int] = None,
concurrency: Optional[int] = None,
) -> List[V]:
input_values = list(input_values) # in case the input is mistakenly not a sequence
return list(
tqdm(
parallel_map(
func=func,
input_values=input_values,
chunk_size=chunk_size,
concurrency=concurrency,
),
total=len(input_values),
dynamic_ncols=True,
)
)