-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
342 additions
and
217 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from concurrent.futures import Future | ||
from dataclasses import dataclass | ||
from functools import singledispatch | ||
from typing import Callable | ||
|
||
|
||
@dataclass | ||
class DependencyResolver: | ||
"""A DependencyResolver describes how app dependencies can be resolved. | ||
It is specified as two functions: `traverse_to_gather` which turns an | ||
app parameter into a list of futures which must be waited for before | ||
the task can be executed (for example, in the case of | ||
`DEEP_DEPENDENCY_RESOLVER` this traverses structures such as lists to | ||
find every contained ``Future``), and `traverse_to_unwrap` which turns an | ||
app parameter into its value to be passed to the app on execution | ||
(for example in the case of `DEEP_DEPENDENCY_RESOLVER` this replaces a | ||
list containing futures with a new list containing the values of those | ||
resolved futures). | ||
By default, Parsl will use `SHALLOW_DEPENDENCY_RESOLVER` which only | ||
resolves Futures passed directly as arguments. | ||
""" | ||
traverse_to_gather: Callable | ||
traverse_to_unwrap: Callable | ||
|
||
|
||
@singledispatch | ||
def shallow_traverse_to_gather(o): | ||
# objects in general do not expose futures that we can see | ||
return [] | ||
|
||
|
||
@singledispatch | ||
def shallow_traverse_to_unwrap(o): | ||
# objects in general unwrap to themselves | ||
return o | ||
|
||
|
||
@shallow_traverse_to_gather.register | ||
def _(fut: Future): | ||
return [fut] | ||
|
||
|
||
@shallow_traverse_to_unwrap.register | ||
@singledispatch | ||
def _(fut: Future): | ||
return fut.result() | ||
|
||
|
||
@singledispatch | ||
def deep_traverse_to_gather(o): | ||
# objects in general do not expose futures that we can see | ||
return [] | ||
|
||
|
||
@singledispatch | ||
def deep_traverse_to_unwrap(o): | ||
# objects in general unwrap to themselves | ||
return o | ||
|
||
|
||
@deep_traverse_to_gather.register | ||
def _(fut: Future): | ||
return [fut] | ||
|
||
|
||
@deep_traverse_to_unwrap.register | ||
@singledispatch | ||
def _(fut: Future): | ||
return fut.result() | ||
|
||
|
||
@deep_traverse_to_gather.register(tuple) | ||
@deep_traverse_to_gather.register(list) | ||
@deep_traverse_to_gather.register(set) | ||
def _(iterable): | ||
return [e for v in iterable for e in deep_traverse_to_gather(v) if isinstance(e, Future)] | ||
|
||
|
||
@deep_traverse_to_unwrap.register(tuple) | ||
@deep_traverse_to_unwrap.register(list) | ||
@deep_traverse_to_unwrap.register(set) | ||
@singledispatch | ||
def _(iterable): | ||
|
||
type_ = type(iterable) | ||
return type_(map(deep_traverse_to_unwrap, iterable)) | ||
|
||
|
||
@deep_traverse_to_gather.register(dict) | ||
def _(dictionary): | ||
futures = [] | ||
for key, value in dictionary.items(): | ||
if isinstance(key, Future): | ||
futures.append(key) | ||
if isinstance(value, Future): | ||
futures.append(value) | ||
return futures | ||
|
||
|
||
@deep_traverse_to_unwrap.register(dict) | ||
def _(dictionary): | ||
unwrapped_dict = {} | ||
for key, value in dictionary.items(): | ||
key = deep_traverse_to_unwrap(key) | ||
value = deep_traverse_to_unwrap(value) | ||
unwrapped_dict[key] = value | ||
return unwrapped_dict | ||
|
||
|
||
DEEP_DEPENDENCY_RESOLVER = DependencyResolver(traverse_to_gather=deep_traverse_to_gather, | ||
traverse_to_unwrap=deep_traverse_to_unwrap) | ||
|
||
SHALLOW_DEPENDENCY_RESOLVER = DependencyResolver(traverse_to_gather=shallow_traverse_to_gather, | ||
traverse_to_unwrap=shallow_traverse_to_unwrap) |
Oops, something went wrong.