Skip to content

API reference · 06 of 8

Rust pump and backends

Whether the optional native pump is present, and what it reports when a pipeline hop uses it or falls back.

Read from Cuprum 0.2.0-beta2 at a4089878

function

observe_pump

cuprum/pump_observation.py, the source of observe_pump
def observe_pump(hook: PumpHook) -> PumpHookRegistration

Register a hook for Rust-pump routing events in the current context.

Parameters

hook
Synchronous callable invoked with each PumpEvent.

Returns

PumpHookRegistration
A handle that can be detached or used as a context manager.

Examples

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()
class

PumpEvent

cuprum/pump_events.py, the source of PumpEvent
class 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.

Attributes

phase
Which routing decision this event reports. See PumpPhase.
reason
For declined, the seam that refused the hand-off. None for every other phase.
outcome
For handoff, the closed outcome of the writer-resource hand-off. None for every other phase.
duration_s
Monotonic seconds spent waiting for the native worker's cleanup when phase is cleanup_completed. None for every other phase.
elapsed_s
Monotonic seconds spent waiting before cleanup_grace_expired. None for every other phase.
exec_id
The upstream pipeline stage's stable execution token for native-cleanup phases. Tracing observers use it only to locate an existing execution span; it is None for every other phase and is never a trace attribute or metric label.

Examples

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
6 members

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
type alias

PumpHook

cuprum/pump_events.py, the source of PumpHook
PumpHook = 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.

class

PumpHookRegistration

cuprum/pump_observation.py, the source of PumpHookRegistration
class 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.

1 member

method PumpHookRegistration.detach

def detach() -> None

Release the registration, exactly once.

class

RustPumpDeclineReason

cuprum/pump_events.py, the source of RustPumpDeclineReason
RustPumpDeclineReason

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.

Examples

The member value is the string operators see::

    assert RustPumpDeclineReason.RAW_FD_UNAVAILABLE == "raw_fd_unavailable"
6 members

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'
class

RustPumpHandoffOutcome

cuprum/pump_events.py, the source of RustPumpHandoffOutcome
RustPumpHandoffOutcome

Bases: enum.StrEnum

A bounded result of attempting the Rust writer-resource hand-off.

10 members

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'
function

observe_pump_span

cuprum/pump_span_observation.py, the source of observe_pump_span
def observe_pump_span(tracer: Tracer) -> PumpHopSpanRegistration

Register a tracer for Rust-pump executor-hop spans.

Parameters

tracer
Backend that opens a span for every Rust-pump hop started in this context.

Returns

PumpHopSpanRegistration
A handle that can detach the tracer or scope it with a with block.
class

PumpHopSpanRegistration

cuprum/pump_span_observation.py, the source of PumpHopSpanRegistration
class 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.

1 member

method PumpHopSpanRegistration.detach

def detach() -> None

Release the registration, exactly once.

class

PumpHopOutcome

cuprum/pump_span_events.py, the source of PumpHopOutcome
PumpHopOutcome

Bases: enum.StrEnum

The bounded terminal outcomes of one Rust-pump executor hop.

4 members

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'