Skip to content

Commit 1305bcd

Browse files
committed
Add cancel_point to the Runner
1 parent c9ce912 commit 1305bcd

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

adaptive/runner.py

+21
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ def _process_futures(self, done_futs):
259259
try:
260260
y = fut.result()
261261
t = time.time() - fut.start_time # total execution time
262+
except asyncio.CancelledError:
263+
# Cleanup
264+
self._to_retry.pop(pid, None)
265+
self._tracebacks.pop(pid, None)
266+
self._id_to_point.pop(pid, None)
262267
except Exception as e:
263268
self._tracebacks[pid] = traceback.format_exc()
264269
self._to_retry[pid] = self._to_retry.get(pid, 0) + 1
@@ -754,6 +759,22 @@ def elapsed_time(self):
754759
end_time = time.time()
755760
return end_time - self.start_time
756761

762+
def cancel_point(
763+
self, point: Any | None = None, future: asyncio.Future | None = None
764+
):
765+
"""Cancel a point that is currently being evaluated.
766+
767+
Parameters
768+
----------
769+
point
770+
The point that should be cancelled.
771+
"""
772+
if point is None and future is None:
773+
raise ValueError("Either point or future must be given")
774+
if future is None:
775+
future = next(fut for fut, p in self.pending_points if p == point)
776+
future.cancel()
777+
757778
def add_periodic_callback(
758779
self,
759780
method: Callable[[AsyncRunner]],

0 commit comments

Comments
 (0)