8
8
import time
9
9
import traceback
10
10
import warnings
11
+ import abc
11
12
12
13
from .notebook_integration import live_plot , live_info , in_ipynb
13
14
from .utils import timed
@@ -53,7 +54,7 @@ def _default_executor(*args, **kwargs):
53
54
_default_executor_kwargs = {}
54
55
55
56
56
- class BaseRunner :
57
+ class BaseRunner ( metaclass = abc . ABCMeta ) :
57
58
"""Base class for runners that use `concurrent.futures.Executors`.
58
59
59
60
Parameters
@@ -241,6 +242,20 @@ def _cleanup(self):
241
242
def failed (self ):
242
243
"""Set of points that failed ``runner.retries`` times."""
243
244
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
244
259
245
260
246
261
class BlockingRunner (BaseRunner ):
@@ -444,11 +459,6 @@ def goal(_):
444
459
raise RuntimeError ('Cannot use an executor when learning an '
445
460
'async function.' )
446
461
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 )
452
462
453
463
self .task = self .ioloop .create_task (self ._run ())
454
464
self .saving_task = None
@@ -458,6 +468,13 @@ def goal(_):
458
468
"in a Jupyter notebook, remember to run "
459
469
"'adaptive.notebook_extension()'" )
460
470
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
+
461
478
def status (self ):
462
479
"""Return the runner status as a string.
463
480
0 commit comments