import adulib.asynchronous as this_module
asynchronous
Utilities for async programming.
is_in_event_loop
is_in_event_loop()
batch_executor (async)
batch_executor(
func: Callable,str, Any],
constant_kwargs: Dict[
batch_args: Optional[Iterable[Tuple[Any, ...]]],str, Any]]],
batch_kwargs: Optional[Iterable[Dict[int],
concurrency_limit: Optional[bool,
verbose: str
progress_bar_desc: )
Executes a batch of asynchronous tasks.
Parameters: - func (Callable): The asynchronous function to execute for each batch. - constant_kwargs (Dict[str, Any], optional): Constant keyword arguments to pass to each function call. - batch_args (Optional[Iterable[Tuple[Any, …]]], optional): Iterable of argument tuples for each function call. - batch_kwargs (Optional[Iterable[Dict[str, Any]]], optional): Iterable of keyword argument dictionaries for each function call. - concurrency_limit (Optional[int], optional): Maximum number of concurrent tasks. If None, no limit is applied. - verbose (bool, optional): If True, displays a progress bar. Default is True. - progress_bar_desc (str, optional): Description for the progress bar. Default is “Processing”.
Returns: - List of results from the executed tasks.
Raises: - ValueError: If both ‘batch_args’ and ‘batch_kwargs’ are empty or if their lengths do not match.
async def sample_function(x, y, z):
await asyncio.sleep(0.1)
return z*(x + y)
= {'z': 10}
constant_kwargs = [(1,), (3,), (5,)]
batch_args = [{'y': 2}, {'y': 4}, {'y': 6}]
batch_kwargs
= await batch_executor(
results =sample_function,
func=constant_kwargs,
constant_kwargs=batch_args,
batch_args=batch_kwargs,
batch_kwargs=2,
concurrency_limit
)
print("Results:", results)
Processing: 0%| | 0/3 [00:00<?, ?it/s]Processing: 33%|██████████████████████████████████████████████████████▋ | 1/3 [00:00<00:00, 9.84it/s]Processing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00, 15.68it/s]Processing: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00, 14.68it/s]
Results: [30, 70, 110]