-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds DaskScheduler * upgrades * no ci for 3.8 * lazy import dask * updates * drop py3.7 support * lockfile * fixes? * future * fixes * fixes * fixes * fixes for 3.8 * update cassettes * ci * changelog and DaskExecutor.from_kwargs * fixes as_completed * refactor Executor type to protocol * fixes protocol * fixes * Update webknossos/Changelog.md Co-authored-by: Philipp Otto <philippotto@users.noreply.github.com> * Changelog --------- Co-authored-by: Philipp Otto <philippotto@users.noreply.github.com>
- Loading branch information
1 parent
1cb7101
commit 14efb6f
Showing
60 changed files
with
3,532 additions
and
5,710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from concurrent.futures import Future | ||
from os import PathLike | ||
from typing import ( | ||
Callable, | ||
ContextManager, | ||
Iterable, | ||
Iterator, | ||
List, | ||
Optional, | ||
Protocol, | ||
TypeVar, | ||
) | ||
|
||
from typing_extensions import ParamSpec | ||
|
||
_T = TypeVar("_T") | ||
_P = ParamSpec("_P") | ||
_S = TypeVar("_S") | ||
|
||
|
||
class Executor(Protocol, ContextManager["Executor"]): | ||
@classmethod | ||
def as_completed(cls, futures: List["Future[_T]"]) -> Iterator["Future[_T]"]: | ||
... | ||
|
||
def submit( | ||
self, | ||
__fn: Callable[_P, _T], | ||
/, | ||
*args: _P.args, | ||
**kwargs: _P.kwargs, | ||
) -> "Future[_T]": | ||
... | ||
|
||
def map_unordered(self, fn: Callable[[_S], _T], args: Iterable[_S]) -> Iterator[_T]: | ||
... | ||
|
||
def map_to_futures( | ||
self, | ||
fn: Callable[[_S], _T], | ||
args: Iterable[_S], | ||
output_pickle_path_getter: Optional[Callable[[_S], PathLike]] = None, | ||
) -> List["Future[_T]"]: | ||
... | ||
|
||
def map( | ||
self, | ||
fn: Callable[[_S], _T], | ||
iterables: Iterable[_S], | ||
timeout: Optional[float] = None, | ||
chunksize: Optional[int] = None, | ||
) -> Iterator[_T]: | ||
... | ||
|
||
def forward_log(self, fut: "Future[_T]") -> _T: | ||
... | ||
|
||
def shutdown(self, wait: bool = True, *, cancel_futures: bool = False) -> None: | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import os | ||
from concurrent import futures | ||
from concurrent.futures import Future | ||
from functools import partial | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Callable, | ||
Iterable, | ||
Iterator, | ||
List, | ||
Optional, | ||
TypeVar, | ||
cast, | ||
) | ||
|
||
from typing_extensions import ParamSpec | ||
|
||
from cluster_tools._utils.warning import enrich_future_with_uncaught_warning | ||
from cluster_tools.executors.multiprocessing_ import CFutDict, MultiprocessingExecutor | ||
|
||
if TYPE_CHECKING: | ||
from distributed import Client | ||
|
||
_T = TypeVar("_T") | ||
_P = ParamSpec("_P") | ||
_S = TypeVar("_S") | ||
|
||
|
||
class DaskExecutor(futures.Executor): | ||
client: "Client" | ||
|
||
def __init__( | ||
self, | ||
client: "Client", | ||
) -> None: | ||
self.client = client | ||
|
||
@classmethod | ||
def from_kwargs( | ||
cls, | ||
**kwargs: Any, | ||
) -> "DaskExecutor": | ||
from distributed import Client | ||
|
||
return cls(Client(**kwargs)) | ||
|
||
@classmethod | ||
def as_completed(cls, futures: List["Future[_T]"]) -> Iterator["Future[_T]"]: | ||
from distributed import as_completed | ||
|
||
return as_completed(futures) | ||
|
||
def submit( # type: ignore[override] | ||
self, | ||
__fn: Callable[_P, _T], | ||
*args: _P.args, | ||
**kwargs: _P.kwargs, | ||
) -> "Future[_T]": | ||
if "__cfut_options" in kwargs: | ||
output_pickle_path = cast(CFutDict, kwargs["__cfut_options"])[ | ||
"output_pickle_path" | ||
] | ||
del kwargs["__cfut_options"] | ||
|
||
__fn = partial( | ||
MultiprocessingExecutor._execute_and_persist_function, | ||
output_pickle_path, | ||
__fn, | ||
) | ||
fut = self.client.submit(partial(__fn, *args, **kwargs)) | ||
|
||
enrich_future_with_uncaught_warning(fut) | ||
return fut | ||
|
||
def map_unordered(self, fn: Callable[[_S], _T], args: Iterable[_S]) -> Iterator[_T]: | ||
futs: List["Future[_T]"] = self.map_to_futures(fn, args) | ||
|
||
# Return a separate generator to avoid that map_unordered | ||
# is executed lazily (otherwise, jobs would be submitted | ||
# lazily, as well). | ||
def result_generator() -> Iterator: | ||
for fut in self.as_completed(futs): | ||
yield fut.result() | ||
|
||
return result_generator() | ||
|
||
def map_to_futures( | ||
self, | ||
fn: Callable[[_S], _T], | ||
args: Iterable[_S], # TODO change: allow more than one arg per call | ||
output_pickle_path_getter: Optional[Callable[[_S], os.PathLike]] = None, | ||
) -> List["Future[_T]"]: | ||
if output_pickle_path_getter is not None: | ||
futs = [ | ||
self.submit( # type: ignore[call-arg] | ||
fn, | ||
arg, | ||
__cfut_options={ | ||
"output_pickle_path": output_pickle_path_getter(arg) | ||
}, | ||
) | ||
for arg in args | ||
] | ||
else: | ||
futs = [self.submit(fn, arg) for arg in args] | ||
|
||
return futs | ||
|
||
def map( # type: ignore[override] | ||
self, | ||
fn: Callable[[_S], _T], | ||
iterables: Iterable[_S], | ||
timeout: Optional[float] = None, | ||
chunksize: Optional[int] = None, | ||
) -> Iterator[_T]: | ||
if chunksize is None: | ||
chunksize = 1 | ||
return super().map(fn, iterables, timeout=timeout, chunksize=chunksize) | ||
|
||
def forward_log(self, fut: "Future[_T]") -> _T: | ||
return fut.result() | ||
|
||
def shutdown(self, wait: bool = True, *, cancel_futures: bool = False) -> None: | ||
if wait: | ||
self.client.close(timeout=60 * 60 * 24) | ||
else: | ||
self.client.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.