Skip to content

Commit 2fc9f85

Browse files
committed
Add cancel_point to the Runner
1 parent f59d38d commit 2fc9f85

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

adaptive/runner.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ def _process_futures(self, done_futs):
223223
try:
224224
y = fut.result()
225225
t = time.time() - fut.start_time # total execution time
226+
except asyncio.CancelledError:
227+
# Cleanup
228+
self._to_retry.pop(pid, None)
229+
self._tracebacks.pop(pid, None)
230+
self._id_to_point.pop(pid, None)
226231
except Exception as e:
227232
self._tracebacks[pid] = traceback.format_exc()
228233
self._to_retry[pid] = self._to_retry.get(pid, 0) + 1
@@ -670,6 +675,22 @@ def elapsed_time(self):
670675
end_time = time.time()
671676
return end_time - self.start_time
672677

678+
def cancel_point(
679+
self, point: Any | None = None, future: asyncio.Future | None = None
680+
):
681+
"""Cancel a point that is currently being evaluated.
682+
683+
Parameters
684+
----------
685+
point
686+
The point that should be cancelled.
687+
"""
688+
if point is None and future is None:
689+
raise ValueError("Either point or future must be given")
690+
if future is None:
691+
future = next(fut for fut, p in self.pending_points if p == point)
692+
future.cancel()
693+
673694
def add_periodic_callback(
674695
self,
675696
method: Callable[[AsyncRunner]],

0 commit comments

Comments
 (0)