Skip to main content
Monty provides snapshot classes that allow you to pause and resume code execution. This is useful for handling external function calls iteratively, implementing async operations, or serializing execution state.

FunctionSnapshot

Represents a paused execution waiting for an external function call return value. Contains information about the pending external function call and allows resuming execution with the return value.

Properties

str
The name of the script being executed
bool
Whether this snapshot is for an OS function call (e.g., Path.stat)
bool
Whether this snapshot is for a dataclass method call (first arg is self)
str | OsFunction
The name of the function being called (external function or OS function like 'Path.stat'). Will be an OsFunction if is_os_function is True.
tuple[Any, ...]
The positional arguments passed to the external function
dict[str, Any]
The keyword arguments passed to the external function
int
The unique identifier for this external function call

Methods

resume()

Resume execution with a return value, exception, or future. Only one of the parameters should be provided. resume() may only be called once on each FunctionSnapshot instance. The GIL is released allowing parallel execution.
Any
The value to return from the external function call
BaseException
An exception to raise in the Monty interpreter
EllipsisType
A future to await in the Monty interpreter. Pass ... (ellipsis) to indicate a pending future.
  • FunctionSnapshot if another external function call is pending
  • NameLookupSnapshot if another name lookup is pending
  • FutureSnapshot if futures need to be resolved
  • MontyComplete if execution finished
Raises:
  • TypeError: If both arguments are provided or if invalid arguments are passed
  • RuntimeError: If execution has already completed or if resume was already called
  • MontyRuntimeError: If the code raises an exception during execution

dump()

Serialize the FunctionSnapshot instance to a binary format. This allows suspending execution and resuming later, potentially in a different process.
Bytes containing the serialized FunctionSnapshot instance
Note: The print_callback is not serialized and must be re-provided via set_print_callback() after loading if print output is needed. Raises:
  • ValueError: If serialization fails
  • RuntimeError: If the progress has already been resumed

load() (static method)

Deserialize a FunctionSnapshot instance from binary format.
bytes
required
The serialized FunctionSnapshot data from dump()
Callable[[Literal['stdout'], str], None] | None
Optional callback for print output
list[type] | None
Optional list of dataclass types to register for proper isinstance() support on output
A new FunctionSnapshot instance
Raises:
  • ValueError: If deserialization fails

Example

MontyComplete

The result of a completed code execution.

Properties

Any
The final output value from the executed code

Example

NameLookupSnapshot

Represents a paused execution waiting for a name lookup to be resolved. This is used in advanced scenarios where variable lookups need external resolution.

Properties

str
The name of the script being executed
str
The name of the variable being looked up

Methods

resume()

Resume execution with the value from a name lookup, if any. If no value is passed, a NameError is raised. resume() may only be called once on each NameLookupSnapshot instance. The GIL is released allowing parallel execution.
Any | None
The value from the name lookup, if any
  • FunctionSnapshot if an external function call is pending
  • NameLookupSnapshot if more futures need to be resolved
  • FutureSnapshot if another name lookup is pending
  • MontyComplete if execution finished
Raises:
  • TypeError: If result dict has invalid keys
  • RuntimeError: If execution has already completed
  • MontyRuntimeError: If the code raises an exception during execution

dump() and load()

Similar to FunctionSnapshot, NameLookupSnapshot also supports serialization:

FutureSnapshot

Represents a paused execution waiting for multiple futures to be resolved. This is used when handling async operations or parallel external function calls.

Properties

str
The name of the script being executed
list[int]
The call IDs of the pending futures. Raises an error if the snapshot has already been resumed.

Methods

resume()

Resume execution with results for one or more futures. resume() may only be called once on each FutureSnapshot instance. The GIL is released allowing parallel execution.
dict[int, ExternalResult]
required
Dict mapping call_id to result dict. Each result dict must have either 'return_value' or 'exception' key (not both).
  • FunctionSnapshot if an external function call is pending
  • NameLookupSnapshot if more futures need to be resolved
  • FutureSnapshot if more futures need to be resolved
  • MontyComplete if execution finished
Raises:
  • TypeError: If result dict has invalid keys
  • RuntimeError: If execution has already completed
  • MontyRuntimeError: If the code raises an exception during execution

dump() and load()

Similar to FunctionSnapshot, FutureSnapshot also supports serialization:

ExternalResult Type

The ExternalResult type is used when resuming FutureSnapshot instances. It’s a union type:

Example with FutureSnapshot

Serialization Example

  • Monty class - Main class that creates snapshots via start()
  • Errors - Error types that can be raised during execution