Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: replace BlockingRunner with AsyncRunner + asyncio.run #230

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 41 additions & 53 deletions adaptive/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,29 @@ def _submit(self, x):
pass


class BlockingRunner(BaseRunner):
def BlockingRunner(
learner,
goal,
*,
executor=None,
ntasks=None,
log=False,
shutdown_executor=False,
retries=0,
raise_if_retries_exceeded=True,
):
"""Run a learner synchronously in an executor.

**This runner is deprecated**, replace:

>>> r = adaptive.BlockingRunner(...)

with

>>> import asyncio
>>> r = adaptive.Runner(...)
>>> r.ioloop.run_until_complete(r.task)

Parameters
----------
learner : `~adaptive.BaseLearner` instance
Expand Down Expand Up @@ -347,62 +367,30 @@ class BlockingRunner(BaseRunner):

"""

def __init__(
self,
if inspect.iscoroutinefunction(learner.function):
raise ValueError("Coroutine functions can only be used " "with 'AsyncRunner'.")
warnings.warn(
"adaptive.BlockingRunner is deprecated, and will be removed in a "
"future version of adaptive.\n"
"Replace all uses of adaptive.BlockingRunner(...) with "
"asyncio.run(adaptive.Runner(...))",
DeprecationWarning,
)

r = AsyncRunner(
learner,
goal,
*,
executor=None,
ntasks=None,
log=False,
shutdown_executor=False,
retries=0,
raise_if_retries_exceeded=True,
):
if inspect.iscoroutinefunction(learner.function):
raise ValueError(
"Coroutine functions can only be used " "with 'AsyncRunner'."
)
super().__init__(
learner,
goal,
executor=executor,
ntasks=ntasks,
log=log,
shutdown_executor=shutdown_executor,
retries=retries,
raise_if_retries_exceeded=raise_if_retries_exceeded,
)
self._run()

def _submit(self, x):
return self.executor.submit(self.learner.function, x)
executor=executor,
ntasks=ntasks,
log=log,
shutdown_executor=shutdown_executor,
retries=retries,
raise_if_retries_exceeded=raise_if_retries_exceeded,
)

def _run(self):
first_completed = concurrent.FIRST_COMPLETED
r.ioloop.run_until_complete(r.task)

if self._get_max_tasks() < 1:
raise RuntimeError("Executor has no workers")

try:
while not self.goal(self.learner):
futures = self._get_futures()
done, _ = concurrent.wait(futures, return_when=first_completed)
self._process_futures(done)
finally:
remaining = self._remove_unfinished()
if remaining:
concurrent.wait(remaining)
self._cleanup()

def elapsed_time(self):
"""Return the total time elapsed since the runner
was started."""
if self.end_time is None:
# This shouldn't happen if the BlockingRunner
# correctly finished.
self.end_time = time.time()
return self.end_time - self.start_time
return r


class AsyncRunner(BaseRunner):
Expand Down