stdlibx.cancel package¶
- exception stdlibx.cancel.CancellationTokenCancelledError[source]¶
Bases:
CancellationTokenErrorRaised when an operation is cancelled via a cancellation token. This exception indicates that the token was explicitly cancelled.
- exception stdlibx.cancel.CancellationTokenError[source]¶
Bases:
ExceptionBase class for all cancellation-related errors. Raised when a cancellation event affects the current operation. Serves as the common superclass for all token error types.
- exception stdlibx.cancel.CancellationTokenTimeoutError[source]¶
Bases:
CancellationTokenErrorRaised when a token is cancelled due to a timeout. This exception indicates that the token’s cancellation was triggered by an internal timeout mechanism (for example via
stdlibx.cancel.with_timeout()).
- class stdlibx.cancel.CancellationToken(*args, **kwargs)[source]¶
Bases:
ProtocolA cooperative cancellation token interface. Represents a handle that can be checked or awaited to respond to cancellation events triggered elsewhere
- get_error()[source]¶
Get the cancellation error if the token has been cancelled.
- Returns:
The Exception if cancelled, or None.
- Return type:
Exception | None
- is_cancelled()[source]¶
Check whether the token has been cancelled.
- Returns:
True if the token has been cancelled, False otherwise.
- Return type:
bool
- raise_if_cancelled()[source]¶
Raise the cancellation error if the token has been cancelled.
- Raises:
Exception – If the token is cancelled.
- class stdlibx.cancel.CancelledToken(*args, **kwargs)[source]¶
Bases:
CancellationToken,ProtocolA token that is already cancelled. This specialization of Token ensures that cancellation has occurred and provides non-optional return types
Example:
1if is_token_cancelled(token): 2 reveal_type(token) # CancelledToken
- get_error()[source]¶
Get the cancellation error.
- Returns:
The Exception associated with this cancelled token.
- Return type:
Exception
- is_cancelled()[source]¶
Always returns True, since the token is cancelled.
- Returns:
True
- Return type:
Literal[True]
- stdlibx.cancel.default_token()[source]¶
Create a default, non-cancellable token. This token will never be cancelled and can be used as a placeholder when no cancellation logic is required.
- Returns:
A default Token that is never cancelled.
- Return type:
- stdlibx.cancel.is_token_cancelled(token)[source]¶
Type guard to check if a token is cancelled. This function refines the type of the given token to CancelledToken for type checkers if the token is indeed cancelled.
- Parameters:
token (CancellationToken) – The token to check.
- Returns:
True if the token is cancelled, False otherwise.
- Return type:
TypeGuard[CancelledToken]
- stdlibx.cancel.with_cancel(parent)[source]¶
Create a new cancellable token linked to a parent token. The returned token can be cancelled independently, and will also be cancelled if the parent token is cancelled.
- Parameters:
parent (CancellationToken) – The parent Token to link cancellation from.
- Returns:
A tuple containing, the new Token and a callable to cancel the token manually
- Return type:
tuple[CancellationToken, Callable[[], None]]
- stdlibx.cancel.with_timeout(parent, timeout)[source]¶
Create a new cancellable token with a timeout, linked to a parent token. The token will be cancelled if either: the parent token is cancelled or the specified timeout elapses.
- Parameters:
parent (CancellationToken) – The parent Token to link cancellation from.
timeout (float) – Timeout duration in seconds before automatic cancellation.
- Returns:
A tuple containing, the new Token and a callable to cancel the token manually
- Return type:
tuple[CancellationToken, Callable[[], None]]