Skip to content

Commit

Permalink
Add cancel_point to the Runner
Browse files Browse the repository at this point in the history
  • Loading branch information
basnijholt committed Nov 23, 2022
1 parent c9ce912 commit 1305bcd
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions adaptive/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ def _process_futures(self, done_futs):
try:
y = fut.result()
t = time.time() - fut.start_time # total execution time
except asyncio.CancelledError:
# Cleanup
self._to_retry.pop(pid, None)
self._tracebacks.pop(pid, None)
self._id_to_point.pop(pid, None)
except Exception as e:
self._tracebacks[pid] = traceback.format_exc()
self._to_retry[pid] = self._to_retry.get(pid, 0) + 1
Expand Down Expand Up @@ -754,6 +759,22 @@ def elapsed_time(self):
end_time = time.time()
return end_time - self.start_time

def cancel_point(
self, point: Any | None = None, future: asyncio.Future | None = None
):
"""Cancel a point that is currently being evaluated.
Parameters
----------
point
The point that should be cancelled.
"""
if point is None and future is None:
raise ValueError("Either point or future must be given")
if future is None:
future = next(fut for fut, p in self.pending_points if p == point)
future.cancel()

def add_periodic_callback(
self,
method: Callable[[AsyncRunner]],
Expand Down

0 comments on commit 1305bcd

Please sign in to comment.