-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1434 from PrefectHQ/new-exec
New executor
- Loading branch information
Showing
11 changed files
with
142 additions
and
138 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
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 |
---|---|---|
@@ -1,75 +1,26 @@ | ||
import datetime | ||
import warnings | ||
from contextlib import contextmanager | ||
from queue import Queue | ||
from typing import Any, Callable, Iterable, Iterator, List | ||
|
||
import dask | ||
import dask.bag | ||
|
||
from prefect.engine.executors.base import Executor | ||
from prefect.engine.executors.dask import LocalDaskExecutor | ||
|
||
|
||
class SynchronousExecutor(Executor): | ||
class SynchronousExecutor(LocalDaskExecutor): | ||
""" | ||
An executor that runs all functions synchronously using `dask`. Note that | ||
this executor is known to occasionally run tasks twice when using multi-level mapping. | ||
""" | ||
|
||
@contextmanager | ||
def start(self) -> Iterator: | ||
""" | ||
Context manager for initializing execution. | ||
Configures `dask` and yields the `dask.config` contextmanager. | ||
""" | ||
with dask.config.set(scheduler="synchronous") as cfg: | ||
yield cfg | ||
|
||
def queue(self, maxsize: int = 0) -> Queue: | ||
q = Queue(maxsize=maxsize) # type: Queue | ||
return q | ||
|
||
def submit(self, fn: Callable, *args: Any, **kwargs: Any) -> dask.delayed: | ||
""" | ||
Submit a function to the executor for execution. Returns a `dask.delayed` object. | ||
Args: | ||
- fn (Callable): function that is being submitted for execution | ||
- *args (Any): arguments to be passed to `fn` | ||
- **kwargs (Any): keyword arguments to be passed to `fn` | ||
Returns: | ||
- dask.delayed: a `dask.delayed` object that represents the computation of `fn(*args, **kwargs)` | ||
""" | ||
return dask.delayed(fn)(*args, **kwargs) | ||
def map(self, fn: Callable, *args: Any) -> List[dask.delayed]: | ||
""" | ||
Submit a function to be mapped over its iterable arguments. | ||
Args: | ||
- fn (Callable): function that is being submitted for execution | ||
- *args (Any): arguments that the function will be mapped over | ||
Returns: | ||
- List[dask.delayed]: the result of computating the function over the arguments | ||
""" | ||
results = [] | ||
for args_i in zip(*args): | ||
results.append(self.submit(fn, *args_i)) | ||
return results | ||
|
||
def wait(self, futures: Any) -> Any: | ||
""" | ||
Resolves a `dask.delayed` object to its values. Blocks until the computation is complete. | ||
Args: | ||
- futures (Any): iterable of `dask.delayed` objects to compute | ||
NOTE: this class is deprecated and maintained only for backwards-compatibility. | ||
""" | ||
|
||
Returns: | ||
- Any: an iterable of resolved futures | ||
""" | ||
with dask.config.set(scheduler="synchronous"): | ||
return dask.compute(futures)[0] | ||
def __init__(self) -> None: | ||
warnings.warn( | ||
"The SynchronousExecutor is deprecated and will be removed from " | ||
"Prefect. Use a LocalDaskExecutor with a 'synchronous' scheduler instead.", | ||
UserWarning, | ||
) | ||
super().__init__(scheduler="synchronous") |
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.