Skip to content

Suspend & Resume

What feed_start (and each resume / resume_auto) yields — a completed run, or a suspension to answer — and the ExternalResult shapes those answers take. See snapshots for the concepts.

MontyComplete

The result of a completed feed_start execution.

Attributes

output

The final value, converted to a Python object on each access.

Type: Any

FunctionSnapshot

A paused execution waiting for an external function or OS call result.

For OS calls is_os_function is True and function_name is the OsFunction name; resume with a value, an exception, or resume_not_handled().

Attributes

object_id

Session uuid of the routed receiver — a host class instance sent via ClassInstance, or a host class sent via ClassType (a classmethod call, or construction, which arrives as __call__); None for plain external functions and OS calls. The receiver is not included in args.

Type: uuid.UUID | None

Methods

resume
def resume(result: ExternalResult) -> SyncSnapshot

Resume with the call’s result; resumes at most once.

Answers only this call: the result is passed straight through, and neither the feed’s mounts nor the captured os= are consulted. Use resume_auto() for those.

Returns

SyncSnapshot

resume_not_handled
def resume_not_handled() -> SyncSnapshot

Resume an OS-call snapshot with monty’s default unhandled behaviour.

Returns

SyncSnapshot

resume_auto
def resume_auto() -> SyncSnapshot

Answer this call automatically, then return the next snapshot (or MontyComplete). Resumes at most once.

An OS call is offered to the feed’s mounts first, falling back to the os= captured at feed_start / load_snapshot and then to monty’s unhandled default. An external call is resolved through external_lookup=; a name absent from it makes the sandbox raise NameError (as in feed_run). A coroutine external raises RuntimeError — use AsyncMonty for async externals.

Returns

SyncSnapshot

dump
def dump() -> bytes

Serialize the suspended worker; restore via MontySession.load_snapshot.

Returns

bytes

NameLookupSnapshot

A paused execution waiting for the value of an undefined name, or — when object_id is set — a lazy attribute lookup on a host-backed object (a ClassInstance instance, or a ClassType class).

Attributes

object_id

Session uuid of the receiver for a lazy attribute lookup; None for a plain undefined-name lookup. An omitted-value resume raises AttributeError (not NameError) for attribute lookups.

Type: uuid.UUID | None

Methods

resume
def resume(*, value: Any = ...) -> SyncSnapshot

Resume by binding the name to value (any value, including None), or omit value to leave the name undefined — the sandbox then raises NameError, or AttributeError when object_id is set (a lazy attribute lookup on a host-backed object).

Returns

SyncSnapshot

resume_auto
def resume_auto() -> SyncSnapshot

Answer this name lookup automatically, then return the next snapshot (or MontyComplete). A plain lookup resolves from the captured external_lookup= (an absent name raises NameError in the sandbox); an object_id lookup resolves through the sending wrapper’s lazy_attrs policy (a denied or absent attribute raises AttributeError; any other exception raised while serving it, or a value that cannot be converted, is raised inside the sandbox).

Returns

SyncSnapshot

dump
def dump() -> bytes

Serialize the suspended worker; restore via MontySession.load_snapshot.

Returns

bytes

FutureSnapshot

A paused execution where every sandbox task is blocked on external futures.

Methods

resume
def resume(results: dict[int, ExternalSettledResult]) -> SyncSnapshot

Resume with settled results for one or more pending futures (by call_id); a future cannot resolve to another future.

Returns

SyncSnapshot

resume_auto
def resume_auto() -> NoReturn

Always raises RuntimeError: a sync session cannot drive coroutine externals. Resolve the pending futures manually with resume({...}), or use AsyncMonty. Does not consume the snapshot.

Returns

NoReturn

dump
def dump() -> bytes

Serialize the suspended worker; restore via MontySession.load_snapshot.

Returns

bytes

AsyncFunctionSnapshot

Async sibling of FunctionSnapshot; resume/resume_not_handled are awaitable.

Attributes

object_id

As FunctionSnapshot.object_id: the routed receiver’s session uuid.

Type: uuid.UUID | None

Methods

resume_auto

@async

def resume_auto() -> AsyncSnapshot

Async sibling of FunctionSnapshot.resume_auto. A coroutine external is spawned and answered with a pending future, so other sandbox tasks keep running; it is later settled by AsyncFutureSnapshot.resume_auto.

Returns

AsyncSnapshot

AsyncNameLookupSnapshot

Async sibling of NameLookupSnapshot.

Attributes

object_id

As NameLookupSnapshot.object_id: the host object a lazy attribute is read from.

Type: uuid.UUID | None

Methods

resume_auto

@async

def resume_auto() -> AsyncSnapshot

Async sibling of NameLookupSnapshot.resume_auto.

Returns

AsyncSnapshot

AsyncFutureSnapshot

Async sibling of FutureSnapshot.

Methods

resume_auto

@async

def resume_auto() -> AsyncSnapshot

Wait for one or more coroutine externals spawned by earlier resume_auto calls to settle, deliver them, and return the next snapshot. Raises if there are no pending coroutines to await (e.g. a snapshot restored via load_snapshot).

Returns

AsyncSnapshot

ExternalReturnValue

Bases: TypedDict

Represents the return value of an external function call.

ExternalException

Bases: TypedDict

Represents an exception raised during an external function call.

ExternalExceptionData

Bases: TypedDict

Represents an exception raised during an external function call by its type and optional message.

Prefer this variant over ExternalException when the caller does not have (or does not want to construct) a concrete Python exception instance — e.g. when resuming a snapshot whose original exception type is not available, or when resuming from another language.

ExternalFuture

Bases: TypedDict

Represents a pending future returned from an external function call.

SyncSnapshot

What MontySession.feed_start (and each sync resume / resume_auto) yields.

Type: TypeAlias Default: FunctionSnapshot | NameLookupSnapshot | FutureSnapshot | MontyComplete

AsyncSnapshot

What AsyncMontySession.feed_start (and each async resume / resume_auto) yields.

Type: TypeAlias Default: AsyncFunctionSnapshot | AsyncNameLookupSnapshot | AsyncFutureSnapshot | MontyComplete

ExternalResult

A caller’s answer to a FunctionSnapshot: a return value, an exception (by instance or by type name), or a pending future.

Default: ExternalSettledResult | ExternalFuture

ExternalSettledResult

A settled answer — a return value or an exception, but never a pending future. Resolving a FutureSnapshot requires settled results: a future cannot resolve to another future.

Default: ExternalReturnValue | ExternalException | ExternalExceptionData