pylegs.io.parallel_function_executor#
- parallel_function_executor(func: Callable, params: Sequence[Dict[str, Any]] = [], workers: int = 2, callback: Callable = None, progress: bool | Callable = True, ignore_error: bool = True, unit: str = 'it', kind: Literal['thread', 'process'] = 'thread', return_values: bool = False)[source]#
Execute a function in parallel using multiple threads.
- Parameters:
- funccallable
The function to be executed in parallel.
- paramssequence of
dict, optional A list of dictionaries containing the keyword arguments to be passed to
funcfor each call. Defaults to an empty list.- workers
int, optional The number of worker threads to use. Defaults to 2.
- callbackcallable, optional
A function to be called with the result of each
funccall. Defaults toNone.- progressbool, optional
- ignore_errorbool, optional
If
True, ignores exceptions raised byfunccalls. Defaults toTrue.- unit
str, optional The unit label to use in the progress bar. Defaults to
'it'.
- Raises:
Notes
The
callbackfunction will be called with the result of each successfulfuncexecution. Ifignore_erroris False and an exception occurs during the execution offunc, the exception will be raised and the remaining tasks will not be executed.Examples
>>> def func(x, y): ... return x + y ... >>> params = [{'x': 1, 'y': 2}, {'x': 3, 'y': 4}] >>> parallel_function_executor(func, params, workers=2, callback=print) 3 7