Skip to content

Commit 99c80fe

Browse files
committed
Merge branch '107-make-baserunner-an-abstract-base-class' into 'master'
Make BaseRunner an abstract base class Closes #107 See merge request qt/adaptive!111
2 parents 16bf8ee + 98b2269 commit 99c80fe

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

adaptive/runner.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import time
99
import traceback
1010
import warnings
11+
import abc
1112

1213
from .notebook_integration import live_plot, live_info, in_ipynb
1314
from .utils import timed
@@ -53,7 +54,7 @@ def _default_executor(*args, **kwargs):
5354
_default_executor_kwargs = {}
5455

5556

56-
class BaseRunner:
57+
class BaseRunner(metaclass=abc.ABCMeta):
5758
"""Base class for runners that use `concurrent.futures.Executors`.
5859
5960
Parameters
@@ -241,6 +242,20 @@ def _cleanup(self):
241242
def failed(self):
242243
"""Set of points that failed ``runner.retries`` times."""
243244
return set(self.tracebacks) - set(self.to_retry)
245+
246+
@abc.abstractmethod
247+
def elapsed_time(self):
248+
"""Return the total time elapsed since the runner
249+
was started.
250+
251+
Is called in `overhead`.
252+
"""
253+
pass
254+
255+
@abc.abstractmethod
256+
def _submit(self, x):
257+
"""Is called in `_get_futures`."""
258+
pass
244259

245260

246261
class BlockingRunner(BaseRunner):
@@ -444,11 +459,6 @@ def goal(_):
444459
raise RuntimeError('Cannot use an executor when learning an '
445460
'async function.')
446461
self.executor.shutdown() # Make sure we don't shoot ourselves later
447-
self._submit = lambda x: self.ioloop.create_task(self.function(x))
448-
else:
449-
self._submit = functools.partial(self.ioloop.run_in_executor,
450-
self.executor,
451-
self.function)
452462

453463
self.task = self.ioloop.create_task(self._run())
454464
self.saving_task = None
@@ -458,6 +468,13 @@ def goal(_):
458468
"in a Jupyter notebook, remember to run "
459469
"'adaptive.notebook_extension()'")
460470

471+
def _submit(self, x):
472+
ioloop = self.ioloop
473+
if inspect.iscoroutinefunction(self.learner.function):
474+
return ioloop.create_task(self.function(x))
475+
else:
476+
return ioloop.run_in_executor(self.executor, self.function, x)
477+
461478
def status(self):
462479
"""Return the runner status as a string.
463480

0 commit comments

Comments
 (0)