|
| 1 | +import os |
| 2 | +from concurrent import futures |
| 3 | +from concurrent.futures import Future |
| 4 | +from functools import partial |
| 5 | +from typing import ( |
| 6 | + TYPE_CHECKING, |
| 7 | + Any, |
| 8 | + Callable, |
| 9 | + Iterable, |
| 10 | + Iterator, |
| 11 | + List, |
| 12 | + Optional, |
| 13 | + TypeVar, |
| 14 | + cast, |
| 15 | +) |
| 16 | + |
| 17 | +from typing_extensions import ParamSpec |
| 18 | + |
| 19 | +from cluster_tools._utils.warning import enrich_future_with_uncaught_warning |
| 20 | +from cluster_tools.executors.multiprocessing_ import CFutDict, MultiprocessingExecutor |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from distributed import Client |
| 24 | + |
| 25 | +_T = TypeVar("_T") |
| 26 | +_P = ParamSpec("_P") |
| 27 | +_S = TypeVar("_S") |
| 28 | + |
| 29 | + |
| 30 | +class DaskExecutor(futures.Executor): |
| 31 | + client: "Client" |
| 32 | + |
| 33 | + def __init__( |
| 34 | + self, |
| 35 | + client: "Client", |
| 36 | + ) -> None: |
| 37 | + self.client = client |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def from_kwargs( |
| 41 | + cls, |
| 42 | + **kwargs: Any, |
| 43 | + ) -> "DaskExecutor": |
| 44 | + from distributed import Client |
| 45 | + |
| 46 | + return cls(Client(**kwargs)) |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def as_completed(cls, futures: List["Future[_T]"]) -> Iterator["Future[_T]"]: |
| 50 | + from distributed import as_completed |
| 51 | + |
| 52 | + return as_completed(futures) |
| 53 | + |
| 54 | + def submit( # type: ignore[override] |
| 55 | + self, |
| 56 | + __fn: Callable[_P, _T], |
| 57 | + *args: _P.args, |
| 58 | + **kwargs: _P.kwargs, |
| 59 | + ) -> "Future[_T]": |
| 60 | + if "__cfut_options" in kwargs: |
| 61 | + output_pickle_path = cast(CFutDict, kwargs["__cfut_options"])[ |
| 62 | + "output_pickle_path" |
| 63 | + ] |
| 64 | + del kwargs["__cfut_options"] |
| 65 | + |
| 66 | + __fn = partial( |
| 67 | + MultiprocessingExecutor._execute_and_persist_function, |
| 68 | + output_pickle_path, |
| 69 | + __fn, |
| 70 | + ) |
| 71 | + fut = self.client.submit(partial(__fn, *args, **kwargs)) |
| 72 | + |
| 73 | + enrich_future_with_uncaught_warning(fut) |
| 74 | + return fut |
| 75 | + |
| 76 | + def map_unordered(self, fn: Callable[[_S], _T], args: Iterable[_S]) -> Iterator[_T]: |
| 77 | + futs: List["Future[_T]"] = self.map_to_futures(fn, args) |
| 78 | + |
| 79 | + # Return a separate generator to avoid that map_unordered |
| 80 | + # is executed lazily (otherwise, jobs would be submitted |
| 81 | + # lazily, as well). |
| 82 | + def result_generator() -> Iterator: |
| 83 | + for fut in self.as_completed(futs): |
| 84 | + yield fut.result() |
| 85 | + |
| 86 | + return result_generator() |
| 87 | + |
| 88 | + def map_to_futures( |
| 89 | + self, |
| 90 | + fn: Callable[[_S], _T], |
| 91 | + args: Iterable[_S], # TODO change: allow more than one arg per call |
| 92 | + output_pickle_path_getter: Optional[Callable[[_S], os.PathLike]] = None, |
| 93 | + ) -> List["Future[_T]"]: |
| 94 | + if output_pickle_path_getter is not None: |
| 95 | + futs = [ |
| 96 | + self.submit( # type: ignore[call-arg] |
| 97 | + fn, |
| 98 | + arg, |
| 99 | + __cfut_options={ |
| 100 | + "output_pickle_path": output_pickle_path_getter(arg) |
| 101 | + }, |
| 102 | + ) |
| 103 | + for arg in args |
| 104 | + ] |
| 105 | + else: |
| 106 | + futs = [self.submit(fn, arg) for arg in args] |
| 107 | + |
| 108 | + return futs |
| 109 | + |
| 110 | + def map( # type: ignore[override] |
| 111 | + self, |
| 112 | + fn: Callable[[_S], _T], |
| 113 | + iterables: Iterable[_S], |
| 114 | + timeout: Optional[float] = None, |
| 115 | + chunksize: Optional[int] = None, |
| 116 | + ) -> Iterator[_T]: |
| 117 | + if chunksize is None: |
| 118 | + chunksize = 1 |
| 119 | + return super().map(fn, iterables, timeout=timeout, chunksize=chunksize) |
| 120 | + |
| 121 | + def forward_log(self, fut: "Future[_T]") -> _T: |
| 122 | + return fut.result() |
| 123 | + |
| 124 | + def shutdown(self, wait: bool = True, *, cancel_futures: bool = False) -> None: |
| 125 | + if wait: |
| 126 | + self.client.close(timeout=60 * 60 * 24) |
| 127 | + else: |
| 128 | + self.client.close() |
0 commit comments