Skip to content

Commit 2176208

Browse files
committed
add implementation of 'stop_after' goal
1 parent 9bb95db commit 2176208

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

adaptive/runner.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,46 @@ def replay_log(learner, log):
712712
getattr(learner, method)(*args)
713713

714714

715+
# --- Useful runner goals
716+
717+
718+
def stop_after(*, seconds=0, minutes=0, hours=0):
719+
"""Stop a runner after a specified time.
720+
721+
For example, to specify a runner that should stop after
722+
5 minutes, one could do the following:
723+
724+
>>> runner = Runner(learner, goal=stop_after(minutes=5))
725+
726+
To stop a runner after 2 hours, 10 minutes and 3 seconds,
727+
one could do the following:
728+
729+
>>> runner = Runner(learner, goal=stop_after(hours=2, minutes=10, seconds=3))
730+
731+
Parameters
732+
----------
733+
seconds, minutes, hours : float, default: 0
734+
If more than one is specified, then they are added together
735+
736+
Returns
737+
-------
738+
goal : callable
739+
Can be used as the ``goal`` parameter when constructing
740+
a `Runner`.
741+
742+
Notes
743+
-----
744+
The duration specified is only a *lower bound* on the time that the
745+
runner will run for, because the runner only checks its goal when
746+
it adds points to its learner
747+
"""
748+
stop_time = time.time() + seconds + 60 * minutes + 3600 * hours
749+
return lambda _: time.time() > stop_time
750+
751+
752+
# -- Internal executor-related, things
753+
754+
715755
class SequentialExecutor(concurrent.Executor):
716756
"""A trivial executor that runs functions synchronously.
717757

0 commit comments

Comments
 (0)