cancellation
Cooperative cancellation for work running on daemon threads.
A daemon thread cannot be stopped from outside, so a run that gives up on one
asks it to stop instead: it sets a RunCancellation the thread can see, and
the thread checks it at points where stopping is safe. The thread then ends by
raising RunCancelledError, and the run waits for it before releasing anything
the thread may still be using.
The current cancellation travels in a context variable. cancellation_scope
binds one for a block, run_on_daemon_thread copies the binding into the thread
it starts (as it copies every context variable) and also registers the thread
with it, so the run can say which threads are still alive and wait for them.
Checking is one call, cheap enough for a per-item loop:
from bitfount.utils.cancellation import raise_if_cancelled
for chunk in chunks:
raise_if_cancelled()
process(chunk)
It does nothing outside a scope, so code that is also run outside a background DAG needs no guard of its own.
Cancellations nest. A child sees its parent's cancellation as its own, so a wave can be cancelled without cancelling its run, while cancelling the run stops the wave too. A thread is registered with every cancellation up the chain, so a run can wait for threads its waves started.
Module
Functions
cancellation_scope
def cancellation_scope( cancellation: RunCancellation,) ‑> Iterator[RunCancellation]:Bind cancellation as the current one for the block.
Arguments
cancellation: The cancellation to bind.
current_cancellation
def current_cancellation() ‑> RunCancellation | None:Return the cancellation bound in this context, or None.
raise_if_cancelled
def raise_if_cancelled() ‑> None:Raise RunCancelledError when the current cancellation is set.
A no-op outside a cancellation_scope.
Raises
RunCancelledError: If the bound cancellation, or a parent, is cancelled.
Classes
RunCancellation
class RunCancellation(parent: RunCancellation | None = None):A cancel flag work on a daemon thread checks, and the threads it covers.
Thread-safe: set from the event loop, read from worker threads.
Arguments
parent: A cancellation whose cancel also cancels this one, orNone.
Create an uncancelled flag under parent.
Variables
cancelled : bool- Whether this cancellation or any parent has been cancelled.
reason : str | None- Why it was cancelled, its own reason first;Nonewhen not cancelled.
Methods
cancel
def cancel(self, reason: str) ‑> None:Cancel, keeping the first reason given.
Arguments
reason: Why, e.g."wave_timeout".
live_threads
def live_threads(self) ‑> list[TrackedThread]:Return the registered threads that are still running.
raise_if_cancelled
def raise_if_cancelled(self) ‑> None:Raise RunCancelledError when cancelled.
Raises
RunCancelledError: If this cancellation or a parent is cancelled.
track
def track(self, thread: threading.Thread) ‑> None:Register thread here and with every parent.
Arguments
thread: A thread started under this cancellation.
wait_for_threads
async def wait_for_threads( self, *, warn_every: float, on_still_waiting: Callable[[list[TrackedThread], float], None] | None = None,) ‑> None:Wait, without blocking the loop, until every registered thread ends.
Unbounded on purpose: whatever the threads hold must not be released while they run. A process that dies stops renewing its leases, which is what bounds this in the end.
Arguments
warn_every: Seconds between calls to on_still_waiting.on_still_waiting: Called with the live threads and the seconds waited so far, every warn_every seconds.
RunCancelledError
class RunCancelledError(reason: str | None):Raised by work that stopped because its run was cancelled.
Arguments
reason: Why the run was cancelled, e.g."wave_timeout".
Build the error for reason.
TrackedThread
class TrackedThread(thread: threading.Thread, started: float):A thread registered with a cancellation, and when it started.
Attributes
thread: The thread.started: When it was registered, intime.monotonicseconds.
Variables
elapsed_seconds : float- Seconds since the thread was registered.
-
started : float
-
thread : threading.Thread