Skip to content

Tell the points still running when an AsyncRunner reaches its goal - #514

Merged
basnijholt merged 2 commits into
python-adaptive:mainfrom
r0c0y:asyncrunner-tell-running-points
Dec 6, 2026
Merged

basnijholt merged 2 commits into
python-adaptive:mainfrom
r0c0y:asyncrunner-tell-running-points

Conversation

@r0c0y

@r0c0y r0c0y commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Description

#319 noticed that the runners threw away the points that were still being computed when the goal was reached, and #320 fixed that for BlockingRunner: it waits for them and tells the learner. AsyncRunner still drops them. As found in #319, it gets an asyncio future from run_in_executor, and cancelling that future marks it cancelled at once, even when the executor's own future is already running and cannot be stopped. The evaluation then finishes in the worker and its result is discarded.

With the default executor (loky), npoints_goal=20, ntasks=4 and evaluations of 0.3 to 0.9 s, 3 runs each:

main this branch
evaluations run 23 23
told to the learner 20 23
run but lost 3 0
runner returns after 3.3 s 3.9 s

So on main about one evaluation in eight of that run was computed for nothing. The extra 0.6 s is the wait for those three points.

AsyncRunner._submit now does what run_in_executor does (executor.submit and asyncio.wrap_future), but keeps the executor's future. When the goal is reached, _run cancels the points that are still queued in the executor and waits for the ones it is already computing, then tells their results, as BlockingRunner does.

runner.cancel() and errors still stop at once, without waiting: a cancel means "stop now", and a running evaluation can take hours. A cancel that arrives during the wait also stops it at once. For an async def function nothing changes, because cancelling its task really stops it.

The goal entry of the AsyncRunner docstring says so.

Tests in test_runner.py:

  • test_points_running_at_the_goal_are_told, for BlockingRunner and AsyncRunner: the upper bound takes 0.5 s and is still running when npoints_goal=10 is reached. It must end up in learner.data, with every evaluation that ran told and nothing pending. On main the AsyncRunner case fails, and BlockingRunner passes since call self._process_futures on canceled futures when BlockingRunner is done #320.
  • test_cancel_does_not_wait_for_running_points: cancel() while a 3 s evaluation is running returns within 1.5 s with status cancelled. This passes on main too. It guards the choice above.

Run here (macOS, on main at 2507701): nox -e pytest_min_deps-3.12 pytest_all_deps-3.12 pytest_min_deps-3.13 pytest_all_deps-3.13 pytest_typeguard pytest_rust_backend all pass (3.11 and 3.14 are not installed here), and pre-commit run --all-files passes, mypy included.

Checklist

  • Fixed style issues using pre-commit run --all (first install using pip install pre-commit)
  • pytest passed

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • (Code) style fix or documentation update
  • This change requires a documentation update

r0c0y and others added 2 commits December 6, 2026 06:37
python-adaptive#320 made BlockingRunner wait for the points the executor is still
computing when the goal is reached and tell their results (python-adaptive#319).
AsyncRunner got its futures from run_in_executor, and cancelling such a
future marks it cancelled at once even when the executor is already
running the point, so the evaluation finished in the worker and was
dropped. Submit to the executor directly, keep its future, and when the
goal is reached cancel only the points that are still queued and tell the
running ones. cancel() and errors still stop at once.

@basnijholt basnijholt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing #319 for AsyncRunner, this is a clean and well-tested improvement!

I pushed a small follow-up commit (d61bba6) to drain _tell_running_points incrementally with asyncio.FIRST_COMPLETED instead of waiting for ALL_COMPLETED, so already-finished points are told immediately (and preserved if runner.cancel() is called during the drain) and fatal errors surface without waiting on slower running points. I also switched the cancellation tests to threading.Event gates and clarified the goal docstring note for time-based goals and shutdown_executor=True.

Looks great to merge once CI is green!

@basnijholt
basnijholt merged commit c34ca2c into python-adaptive:main Dec 6, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants