is_rust_available
cuprum/_backend.py, the source of is_rust_available
def is_rust_available() -> bool
Report cached Rust availability, honouring testing overrides for dispatch.
API reference · 06 of 8
Whether the optional native pump is present, and what it reports when a pipeline hop uses it or falls back.
is_rust_availabledef is_rust_available() -> bool
Report cached Rust availability, honouring testing overrides for dispatch.
observe_pumpdef observe_pump(hook: PumpHook) -> PumpHookRegistration
Register a hook for Rust-pump routing events in the current context.
hookPumpEvent.Count declined hand-offs into a metrics collector::
from cuprum.adapters.metrics_adapter import InMemoryMetrics
from cuprum.adapters.pump_metrics import PumpMetricsHook
metrics = InMemoryMetrics()
with observe_pump(PumpMetricsHook(metrics)):
pipeline.run_sync()
PumpEventclass PumpEvent(
phase: PumpPhase,
reason: RustPumpDeclineReason | None = None,
outcome: RustPumpHandoffOutcome | None = None,
duration_s: float | None = None,
elapsed_s: float | None = None,
exec_id: ExecId | None = None,
)
A Rust-pump routing decision reported to registered pump hooks.
The event carries the phase, a closed-set reason or hand-off outcome, the monotonic wait duration for completed native-pump cleanup, the monotonic elapsed grace wait, and the source stage token needed to correlate cleanup tracing. Descriptor numbers, command arguments, exception types, and tracebacks are all either unbounded as metric labels or a disclosure risk, and the DEBUG log records emitted alongside these events carry the diagnostic detail — including the pump's traceback — for the cases that need it.
phasePumpPhase.reasondeclined, the seam that refused the hand-off. None for every other phase.outcomehandoff, the closed outcome of the writer-resource hand-off. None for every other phase.duration_sphase is cleanup_completed. None for every other phase.elapsed_scleanup_grace_expired. None for every other phase.exec_idNone for every other phase and is never a trace attribute or metric label.A decline names the seam that refused::
event = PumpEvent(
phase="declined",
reason=RustPumpDeclineReason.READER_PAUSE_FAILED,
)
assert event.reason == "reader_pause_failed"
A cancellation-masked failure carries no reason::
assert PumpEvent(phase="failed_after_cancel").reason is None
A completed cleanup carries its monotonic duration::
cleanup = PumpEvent(phase="cleanup_completed", duration_s=0.25)
assert cleanup.duration_s == 0.25
field PumpEvent.phase
phase: PumpPhase
field PumpEvent.reason
reason: RustPumpDeclineReason | None
field PumpEvent.outcome
outcome: RustPumpHandoffOutcome | None
field PumpEvent.duration_s
duration_s: float | None
field PumpEvent.elapsed_s
elapsed_s: float | None
field PumpEvent.exec_id
exec_id: ExecId | None
PumpHookPumpHook = cabc.Callable[[PumpEvent], None]
A synchronous consumer of PumpEvent values.
Pump hooks are synchronous by contract. Every emission site is synchronous — one on the Python-pump fall-back path and the others inside cancellation unwinding — so there is no task to attach an awaitable to and no point at which awaiting it could be ordered against the transfer it describes. A hook that returns an awaitable is reported and discarded rather than run.
PumpHookRegistrationclass PumpHookRegistration(hook: PumpHook) -> None
Bases: _TokenTupleRegistration['PumpHook']
Registration handle for a Rust-pump observation hook.
Supports detach() and context-manager use. The handle captures a contextvars.Token when it installs the extended hook tuple, and detach restores the exact tuple that was current when the handle was created — the same discipline _TokenRegistration applies to execution contexts. Detach in the contextvars.Context that created the registration; resetting a ContextVar with a token from another context raises ValueError.
Prefer with blocks, which detach in last-in-first-out order; detaching out of order restores a tuple that discards later registrations.
method PumpHookRegistration.detach
def detach() -> None
Release the registration, exactly once.
RustPumpDeclineReasonRustPumpDeclineReason
Bases: enum.StrEnum
Why an inter-stage hop fell back from the Rust pump to the Python one.
A closed set rather than free text. Operators filter on cuprum_reason and aggregate the reason metric label, so a typo at a new call site would produce a value their filters silently miss; as an enum it is a type error instead. Members are str, so the logged field and the metric label stay plain strings. PLATFORM_UNSUPPORTED means the current operating system and asyncio transport combination cannot safely use the native Rust pump. DUPLICATE_FDS_UNAVAILABLE means a transport's pipe description could not be re-opened or duplicated for the native worker: the descriptors existed when extraction read them, but one was already closed by the time duplication re-opened it.
The member value is the string operators see::
assert RustPumpDeclineReason.RAW_FD_UNAVAILABLE == "raw_fd_unavailable"
member RustPumpDeclineReason.RAW_FD_UNAVAILABLE
RAW_FD_UNAVAILABLE = 'raw_fd_unavailable'
member RustPumpDeclineReason.READER_UNRESUMABLE
READER_UNRESUMABLE = 'reader_unresumable'
member RustPumpDeclineReason.READER_PAUSE_FAILED
READER_PAUSE_FAILED = 'reader_pause_failed'
member RustPumpDeclineReason.BLOCKING_MODE_UNAVAILABLE
BLOCKING_MODE_UNAVAILABLE = 'blocking_mode_unavailable'
member RustPumpDeclineReason.DUPLICATE_FDS_UNAVAILABLE
DUPLICATE_FDS_UNAVAILABLE = 'duplicate_fds_unavailable'
member RustPumpDeclineReason.PLATFORM_UNSUPPORTED
PLATFORM_UNSUPPORTED = 'platform_unsupported'
RustPumpHandoffOutcomeRustPumpHandoffOutcome
Bases: enum.StrEnum
A bounded result of attempting the Rust writer-resource hand-off.
member RustPumpHandoffOutcome.SUBMITTED
SUBMITTED = 'submitted'
member RustPumpHandoffOutcome.DUPLICATE_WRITER_FAILED
DUPLICATE_WRITER_FAILED = 'duplicate_writer_failed'
member RustPumpHandoffOutcome.BLOCKING_SETUP_FAILED
BLOCKING_SETUP_FAILED = 'blocking_setup_failed'
member RustPumpHandoffOutcome.EXECUTOR_SUBMISSION_REJECTED
EXECUTOR_SUBMISSION_REJECTED = 'executor_submission_rejected'
member RustPumpHandoffOutcome.NATIVE_LOAD_FAILED
NATIVE_LOAD_FAILED = 'native_load_failed'
member RustPumpHandoffOutcome.READER_PREPARATION_FAILED
READER_PREPARATION_FAILED = 'reader_preparation_failed'
member RustPumpHandoffOutcome.BUFFER_VALIDATION_FAILED
BUFFER_VALIDATION_FAILED = 'buffer_validation_failed'
member RustPumpHandoffOutcome.PLATFORM_WRITER_TRANSFER_FAILED
PLATFORM_WRITER_TRANSFER_FAILED = 'platform_writer_transfer_failed'
member RustPumpHandoffOutcome.WINDOWS_RAW_HANDLE_REJECTED
WINDOWS_RAW_HANDLE_REJECTED = 'windows_raw_handle_rejected'
member RustPumpHandoffOutcome.NATIVE_IO_FAILED
NATIVE_IO_FAILED = 'native_io_failed'
observe_pump_spandef observe_pump_span(tracer: Tracer) -> PumpHopSpanRegistration
Register a tracer for Rust-pump executor-hop spans.
tracerwith block.PumpHopSpanRegistrationclass PumpHopSpanRegistration(tracer: Tracer) -> None
Bases: _TokenTupleRegistration['Tracer']
Registration handle for one Rust-pump hop tracer.
The handle restores the precise preceding registration tuple when detached. It should therefore be detached in the contextvars.Context that created it and in last-in-first-out order when registrations nest.
Unlike the other registrations built on _TokenTupleRegistration, this one refuses an out-of-order detach rather than quietly discarding whatever a later registration added: it keeps the tuple it installed so _release can compare it against the live one first.
method PumpHopSpanRegistration.detach
def detach() -> None
Release the registration, exactly once.
PumpHopOutcomePumpHopOutcome
Bases: enum.StrEnum
The bounded terminal outcomes of one Rust-pump executor hop.
member PumpHopOutcome.SUCCEEDED
SUCCEEDED = 'succeeded'
member PumpHopOutcome.FAILED
FAILED = 'failed'
member PumpHopOutcome.CANCELLED
CANCELLED = 'cancelled'
member PumpHopOutcome.FAILED_AFTER_CANCEL
FAILED_AFTER_CANCEL = 'failed_after_cancel'