run_tasks()¶
- audeer.run_tasks(task_func, params, *, num_workers=1, multiprocessing=False, progress_bar=False, task_description=None, maximum_refresh_time=None)[source]¶
Run parallel tasks using multprocessing.
Note
Result values are returned in order of
params
.- Parameters
task_func (
Callable
) – task function with one or more parameters, e.g.x, y, z
, and optionally returning a valueparams (
Sequence
[Tuple
[Sequence
[Any
],Dict
[str
,Any
]]]) – sequence of tuples holding parameters for each task. Each tuple contains a sequence of positional arguments and a dictionary with keyword arguments, e.g.:[((x1, y1), {'z': z1}), ((x2, y2), {'z': z2}), ...]
num_workers (
int
) – number of parallel jobs or 1 for sequential processing. IfNone
will be set to the number of processors on the machine multiplied by 5 in case of multithreading and number of processors in case of multiprocessingmultiprocessing (
bool
) – use multiprocessing instead of multithreadingprogress_bar (
bool
) – show a progress bartask_description (
Optional
[str
]) – task description that will be displayed next to progress barmaximum_refresh_time (
Optional
[float
]) – refresh the progress bar at least everymaximum_refresh_time
seconds, using another thread. IfNone
, no refreshing is enforced
- Return type
- Returns
list of computed results
Examples
>>> power = lambda x, n: x**n >>> params = [([2, n], {}) for n in range(10)] >>> run_tasks(power, params, num_workers=3) [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]