Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Python API Reference

The Python bindings provide a Pythonic interface to Eryx functionality.

For detailed API documentation, see docs.eryx.run/latest/api/python/.

Core Classes

  • Sandbox - Main class for isolated Python execution
  • Session - Persistent state across executions
  • SandboxFactory - Pre-initialize sandboxes with packages
  • SandboxPool - Bounded pool of warm sandboxes for concurrent execution
  • PooledSandbox - A sandbox lease from a pool (context manager)
  • PoolStats - Pool usage statistics
  • VfsStorage - Virtual filesystem storage
  • ResourceLimits - Configure execution constraints
  • NetConfig - Configure network access
  • CallbackRegistry - Decorator-based callback registration
  • CancelToken - Cancel an execution from any thread
  • ReplayOutcome - Result of Sandbox.execute_with_journal() (result/error, journal, suspension)
  • SuspendedCallback - Details of a callback that suspended execution
  • SuspendCallback - Exception a callback raises to suspend execution

Installation

pip install pyeryx

Alternatively, see the PyPI package page.

Returning a structured result

Assign a variable named result in the executed script and Eryx JSON-serializes it and returns it on ExecuteResult.result — a structured channel separate from stdout:

import eryx

sandbox = eryx.Sandbox()
out = sandbox.execute('result = {"answer": 42, "items": [1, 2, 3]}')
print(out.result)  # {'answer': 42, 'items': [1, 2, 3]}

If the value is not JSON-serializable, result is None and result_error explains why — execution still succeeds. Pass result_variable="name" to Sandbox(...) or Session(...) to capture a different variable name.

Async execution and cancellation

execute_async() on Sandbox, Session and PooledSandbox returns an awaitable, so the asyncio event loop keeps running while code executes. Concurrent calls on one Session run one at a time, in call order.

Pass cancel=CancelToken() to execute() or execute_async() and call token.cancel() from any thread to stop the run; it raises eryx.CancelledError. Cancelling the asyncio task also stops the run, and Ctrl-C stops a blocking execute() and raises KeyboardInterrupt. A cancelled Session run loses its Python state, and the next call starts from a fresh interpreter.

import asyncio
import eryx

async def main():
    token = eryx.CancelToken()
    asyncio.get_running_loop().call_later(1.0, token.cancel)
    try:
        await eryx.Sandbox().execute_async("while True: pass", cancel=token)
    except eryx.CancelledError:
        print("cancelled")

asyncio.run(main())

Callback replay & suspension

Sandbox.execute_with_journal(code) records every callback result in a journal (a JSON-compatible dict). Pass that journal back as execute_with_journal(code, journal=...) and callbacks that match it return the recorded result instead of running live. A callback can raise eryx.SuspendCallback(reason) to halt the run; resume later by replaying the journal from the suspended run. Session and pooled sandboxes have the same method.

import eryx

def approve(item: str):
    if not is_approved(item):
        raise eryx.SuspendCallback(f"awaiting approval for {item}")
    return True

callbacks = [{"name": "fetch", "fn": fetch}, {"name": "approve", "fn": approve}]
sandbox = eryx.Sandbox(callbacks=callbacks)
outcome = sandbox.execute_with_journal(code)

if outcome.suspended:
    # Later, once approved: completed callbacks replay, `approve` runs live.
    outcome = sandbox.execute_with_journal(code, journal=outcome.journal)

execute_with_journal does not raise for execution failures; check outcome.error. Journals are a trusted input (replayed values reach the sandbox verbatim), and calls that are not replayed — including the suspended call on resume — run live again, so side-effecting callbacks must be idempotent. See the Callback Replay & Suspension guide.