Skip to content

Commit

Permalink
Add afar.get, which automatically gathers data locally
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw committed Jul 16, 2021
1 parent eeba8e3 commit 10ebd80
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ Outside the context, `result` is a [Dask Future](https://docs.dask.org/en/latest

By default, only the last assignment is saved. One can specify which variables to save:
```python
with afar.run("a", "b"), remotely:
a = 1
b = a + 1
with afar.run("one", "two"), remotely:
one = 1
two = one + 1
```
`a` and `b` are now both Futures. They can be used directly in other `afar.run` contexts:
`one` and `two` are now both Futures. They can be used directly in other `afar.run` contexts:
```python
with afar.run as data, remotely:
c = a + b
three = one + two

assert c.result() == 3
assert data["c"].result() == 3
assert three.result() == 3
assert data["three"].result() == 3
```
`data` is a dictionary of variable names to Futures. It may be necessary at times to get the data from here.

If you want to automatically gather the data locally (to avoid calling `.result()`), use `afar.get` instead of `afar.run`:
```python
with afar.get, remotely:
five = two + three
assert five == 5
```

### Is this a good idea?

I don't know!
Expand Down
2 changes: 1 addition & 1 deletion afar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .core import run, remotely, locally # noqa
from .core import run, get, remotely, locally # noqa

from ._version import get_versions

Expand Down
23 changes: 20 additions & 3 deletions afar/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __call__(self, **submit_kwargs):


class Run:
_gather_data = False

def __init__(self, *names):
self.names = names
self._results = None
Expand All @@ -51,7 +53,7 @@ def __init__(self, *names):
self._with_lineno = None

def __call__(self, *names):
return Run(*names)
return type(self)(*names)

def __enter__(self):
self._frame = inspect.currentframe().f_back
Expand Down Expand Up @@ -149,8 +151,16 @@ def _exit(self, exc_type, exc_value, exc_traceback):
remote_dict = client.submit(
run_on_worker, self._scoped, names, futures, **submit_kwargs
)
for name in names:
self._results[name] = client.submit(getitem, remote_dict, name, **submit_kwargs)
if self._gather_data:
futures_to_name = {
client.submit(getitem, remote_dict, name, **submit_kwargs): name
for name in names
}
for future, result in distributed.as_completed(futures_to_name, with_results=True):
self._results[futures_to_name[future]] = result
else:
for name in names:
self._results[name] = client.submit(getitem, remote_dict, name, **submit_kwargs)
else:
# Run locally. This is handy for testing and debugging.
results = self._scoped()
Expand All @@ -163,10 +173,17 @@ def _exit(self, exc_type, exc_value, exc_traceback):
return True


class Get(Run):
"""Unlike ``run``, ``get`` automatically gathers the data locally"""

_gather_data = True


def run_on_worker(sfunc, names, futures):
sfunc = sfunc.bind(futures)
results = sfunc()
return {key: results[key] for key in names}


run = Run()
get = Get()
20 changes: 15 additions & 5 deletions afar/tests/test_remotely.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import afar
from operator import add
from dask.distributed import Client
import subprocess
import sys

# TODO: better testing infrastructure
if __name__ == "__main__":
import afar
from operator import add
from dask.distributed import Client

client = Client()
two = client.submit(add, 1, 1)

with afar.run, remotely:
with afar.run as results, afar.remotely:
three = two + 1

assert three.result() == 3

with afar.get, afar.remotely(priority=1):
five = two + three
assert five == 5


def test_runme():
assert subprocess.check_call([sys.executable, __file__]) == 0

0 comments on commit 10ebd80

Please sign in to comment.