Dispatch tasks to Dask and iterate over results as they complete. This helper: - Creates (or reuses) a Dask client based on the global/explicit config. - Optionally registers a per-worker environment via `setup_fn` and makes its returned dict available to `func` automaticall
(
func: Callable,
args: Iterable,
client: Optional[Client] = None,
setup_fn: Optional[Callable[[], dict]] = None,
**kwargs: Any,
)
| 451 | |
| 452 | |
| 453 | def parallel_for( |
| 454 | func: Callable, |
| 455 | args: Iterable, |
| 456 | client: Optional[Client] = None, |
| 457 | setup_fn: Optional[Callable[[], dict]] = None, |
| 458 | **kwargs: Any, |
| 459 | ) -> Generator: |
| 460 | """Dispatch tasks to Dask and iterate over results as they complete. |
| 461 | |
| 462 | This helper: |
| 463 | - Creates (or reuses) a Dask client based on the global/explicit config. |
| 464 | - Optionally registers a per-worker environment via `setup_fn` and makes |
| 465 | its returned dict available to `func` automatically. |
| 466 | - Performs a **client-side** conflict check so that keyword arguments |
| 467 | explicitly passed via `kwargs` don't collide with environment-provided |
| 468 | arguments (pre-submission). |
| 469 | - Wraps `func` with `wrap_func_with_worker_env` so any missing keyword |
| 470 | parameters can be injected from the worker environment. |
| 471 | |
| 472 | Iteration order: |
| 473 | Results are yielded in **completion order** (using `as_completed`), |
| 474 | not in the original order of `args`. |
| 475 | |
| 476 | Args: |
| 477 | func: |
| 478 | The function to run on each element of `args`. It may accept |
| 479 | positional and/or keyword parameters. |
| 480 | args: |
| 481 | Iterable of inputs to `func`. |
| 482 | client (optional): |
| 483 | Existing Dask `Client`. If `None`, a temporary client is created |
| 484 | via `get_client` and shut down automatically when iteration ends. |
| 485 | setup_fn (optional): |
| 486 | A callable executed once per worker that returns a `dict` of |
| 487 | environment variables. Keys that match parameter names of `func` |
| 488 | (or any keys if `func` accepts `**kwargs`) are auto-injected |
| 489 | unless explicitly provided in `kwargs`. |
| 490 | **kwargs: |
| 491 | Extra keyword arguments forwarded to every call of `func`. |
| 492 | |
| 493 | Yields: |
| 494 | Each task's result as soon as it finishes. |
| 495 | |
| 496 | Raises: |
| 497 | ValueError: |
| 498 | If at least one key in `kwargs` conflicts with keys provided by |
| 499 | the worker environment (pre-submit check). |
| 500 | Exception: |
| 501 | Any exception raised by `func` is re-raised at iteration time when |
| 502 | accessing `future.result()` for the failing task. |
| 503 | |
| 504 | Notes: |
| 505 | - This generator will close the internally created client once |
| 506 | iteration finishes or the generator is exhausted. |
| 507 | - Worker-side injection and an additional conflict check are also |
| 508 | enforced by `wrap_func_with_worker_env`. |
| 509 | |
| 510 | Example: |
searching dependent graphs…