Apply a function to an iterable of inputs in parallel using Dask. This helper takes care of: - Creating (or reusing) a Dask client according to the global or provided configuration. - Optionally registering a per-worker environment via `setup_fn`, making its returned
(
func: Callable[[Any], Any],
data: Iterable[Any],
client: Optional[Client] = None,
setup_fn: Optional[Callable[[], dict]] = None,
**kwargs: Any,
)
| 380 | |
| 381 | @typechecked |
| 382 | def parallel_map( |
| 383 | func: Callable[[Any], Any], |
| 384 | data: Iterable[Any], |
| 385 | client: Optional[Client] = None, |
| 386 | setup_fn: Optional[Callable[[], dict]] = None, |
| 387 | **kwargs: Any, |
| 388 | ) -> list: |
| 389 | """Apply a function to an iterable of inputs in parallel using Dask. |
| 390 | |
| 391 | This helper takes care of: |
| 392 | - Creating (or reusing) a Dask client according to the global or |
| 393 | provided configuration. |
| 394 | - Optionally registering a per-worker environment via `setup_fn`, |
| 395 | making its returned dictionary available to `func` automatically. |
| 396 | - Detecting and preventing conflicts between explicit `kwargs` and |
| 397 | environment-provided arguments on the **client side** before |
| 398 | submitting tasks. |
| 399 | - Wrapping `func` with `wrap_func_with_worker_env` so that missing |
| 400 | keyword arguments can be injected from the worker environment. |
| 401 | |
| 402 | Args: |
| 403 | func (Callable[[Any], Any]): |
| 404 | The function to execute on each element of `data`. May take |
| 405 | positional and/or keyword parameters. |
| 406 | data (Iterable[Any]): |
| 407 | Iterable of input elements to process. |
| 408 | client (Optional[Client], default=None): |
| 409 | An existing Dask client to use. If `None`, a temporary client |
| 410 | will be created using `get_client` and shut down afterwards. |
| 411 | setup_fn (Optional[Callable[[], dict]], default=None): |
| 412 | A function run once per worker that returns a dictionary of |
| 413 | environment variables. These variables are automatically |
| 414 | injected into `func` if they match parameter names and are not |
| 415 | explicitly provided. |
| 416 | **kwargs: |
| 417 | Additional keyword arguments to pass directly to `func` for all |
| 418 | elements. |
| 419 | |
| 420 | Returns: |
| 421 | list: |
| 422 | The results of applying `func` to each element of `data`, in |
| 423 | order. The list has the same length as `data`. |
| 424 | |
| 425 | Raises: |
| 426 | ValueError: |
| 427 | If any keyword argument name in `kwargs` is also present in the |
| 428 | worker environment keys (conflict detected before submission). |
| 429 | |
| 430 | Example: |
| 431 | >>> def setup_fn(): |
| 432 | ... return {"bias": 10} |
| 433 | >>> def add_bias(x, bias): |
| 434 | ... return x + bias |
| 435 | >>> # Automatic injection of 'bias' from worker environment: |
| 436 | >>> results = parallel_map(add_bias, [1, 2, 3], setup_fn=setup_fn) |
| 437 | >>> results |
| 438 | [11, 12, 13] |
| 439 | """ |
searching dependent graphs…