← All examples

Sandboxes

Sandboxed code execution

A Sandbox is a container you drive from Python: run code and processes, read and write files, expose ports, snapshot memory, throw it away. Each one runs in gVisor with CPU, memory and network limits, which is what makes it safe for code a model wrote.

CPU by default; gpu= for sandboxes that need one. Network can be blocked or limited to CIDR ranges.

How it works

  1. 1Sandbox(...).create() starts an isolated container and returns an instance; keep_warm_seconds decides how long it lives idle.
  2. 2instance.process.run_code(code) runs Python and returns stdout and the exit code; process.exec runs any command.
  3. 3instance.fs reads, writes, uploads, downloads and searches files; expose_port gives a public URL to a process inside.
  4. 4snapshot_memory() and create_from_memory_snapshot() restore a warm sandbox in seconds; every call has an .aio() twin.

The code

sandbox.py
from levers9 import Blueprint, Sandbox

box = Sandbox(cpu=1, memory="1Gi", image=Blueprint(python_packages=["numpy"]), block_network=True).create()
r = box.process.run_code("import numpy as np; print(np.arange(3).sum())")
print(r.result, r.exit_code)      # "3", 0
box.fs.write_text("/workspace/notes.md", "# hello")
box.terminate()
run it
python sandbox.py
levers9 container list            # the sandbox while it runs

Where to go next

Related