Skip to main content

Overview

Monty supports iterative execution, allowing you to pause and resume Python code execution. This is useful for:
  • Handling external function calls with custom logic
  • Implementing async external functions
  • Debugging and step-through execution
  • Dynamic name resolution
When you call Monty.start() or resume() on a snapshot, execution returns one of three types:
  • MontySnapshot - Paused at an external function call
  • MontyNameLookup - Paused at an undefined name lookup
  • MontyComplete - Execution finished successfully

MontySnapshot

Represents paused execution waiting for an external function call to be resolved.

Properties

string
The name of the script being executed
string
The name of the external function being called
any[]
Positional arguments passed to the external function
Record<string, any>
Keyword arguments passed to the external function as an object

Methods

resume()

Resumes execution with either a return value or an exception.
ResumeOptions
required
Must contain either returnValue or exception, but not both
MontySnapshot | MontyNameLookup | MontyComplete
  • MontySnapshot - Paused at another external function call
  • MontyNameLookup - Paused at a name lookup
  • MontyComplete - Execution completed
Throws:
  • MontyRuntimeError - If the code raises an exception

dump()

Serializes the snapshot to binary format for later restoration.
Buffer
Binary representation of the snapshot

load() (static)

Deserializes a snapshot from binary format.
Buffer
required
Binary data from dump()
SnapshotLoadOptions
Optional configuration for loading
MontySnapshot
Restored snapshot ready to resume

Example: Basic Iteration

Example: Error Injection

Example: Serialization

MontyNameLookup

Represents paused execution waiting for a name (variable) to be resolved.

Properties

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

Methods

resume()

Resumes execution after resolving the name lookup.
NameLookupResumeOptions
MontySnapshot | MontyNameLookup | MontyComplete
  • MontySnapshot - Paused at an external function call
  • MontyNameLookup - Paused at another name lookup
  • MontyComplete - Execution completed

dump() / load()

Same as MontySnapshot - serialize and deserialize name lookup state.

Example: Dynamic Name Resolution

MontyComplete

Represents successfully completed execution with a final output value.

Properties

any
The final result value from the executed code (the result of the last expression)

Example

Complete Example: Async External Functions

Combining snapshots with async functions:
For simple async function support, use the built-in runMontyAsync() helper instead of manually handling snapshots. See Monty Class.

See Also