raise_if_canceled#
- rapidtools.core.raise_if_canceled(cancel_event: Event | None, context: str = '') None[source]#
Raise
OperationCanceledifcancel_eventhas been set.Components call this between units of work so that a caller running them on a background thread can stop them promptly. Passing
Nonedisables cancellation entirely, which keeps the call sites free ofifguards.- Parameters:
- Raises:
OperationCanceled – If
cancel_eventis notNoneand is set.
Example
>>> import threading >>> from rapidtools.core import raise_if_canceled >>> >>> stop = threading.Event() >>> raise_if_canceled(stop) # not set: returns silently >>> raise_if_canceled(None) # cancellation disabled: returns silently >>> stop.set() >>> raise_if_canceled(stop, context='inference') Traceback (most recent call last): ... OperationCanceled: Operation canceled during inference.