Getting Started
Cookbook
Real working snippets for common Iris patterns
Run a shell command and check output
import { Sandbox } from '@iris/sdk'
const sandbox = await Sandbox.create()
const result = await sandbox.exec.run('python3 --version')
if (result.ok) {
console.log(result.stdout.trim()) // "Python 3.x.x"
} else {
console.error('Failed:', result.stderr)
}
await sandbox.delete()from iris import IrisClient
client = IrisClient()
sandbox = client.sandboxes.create()
result = sandbox.exec.run(["python3", "--version"])
if result.ok:
print(result.stdout.strip()) # "Python 3.x.x"
else:
print("Failed:", result.stderr)
sandbox.delete()Write a file, execute it, read the output
const sandbox = await Sandbox.create()
await sandbox.files.write('/tmp/hello.py', 'print("hello from iris")\n')
const result = await sandbox.exec.run({ cmd: ['python3', '/tmp/hello.py'], dir: '/tmp' })
console.log(result.stdout) // "hello from iris\n"
await sandbox.delete()sandbox = client.sandboxes.create()
sandbox.files.write("/tmp/hello.py", 'print("hello from iris")\n')
result = sandbox.exec.run(["python3", "/tmp/hello.py"], dir="/tmp")
print(result.stdout) # "hello from iris\n"
sandbox.delete()Fork for safe experimentation
const sandbox = await Sandbox.create()
await sandbox.exec.run('npm install express')
const branch = await sandbox.fork()
await branch.exec.run('rm -rf node_modules')
await branch.delete()
// original still has node_modules
const check = await sandbox.exec.run('ls node_modules | wc -l')
console.log(check.stdout.trim()) // non-zero
await sandbox.delete()sandbox = client.sandboxes.create()
sandbox.exec.run(["bash", "-c", "npm install express"])
branch = sandbox.fork()
branch.exec.run(["bash", "-c", "rm -rf node_modules"])
branch.delete()
# original still has node_modules
check = sandbox.exec.run(["bash", "-c", "ls node_modules | wc -l"])
print(check.stdout.strip()) # non-zero
sandbox.delete()Parallel experiments from the same base
const sandbox = await Sandbox.create()
await sandbox.exec.run('pip install numpy')
const configs = [
{ lr: '0.001', epochs: '10' },
{ lr: '0.01', epochs: '10' },
{ lr: '0.1', epochs: '10' },
]
const results = await Promise.all(
configs.map(async ({ lr, epochs }) => {
const branch = await sandbox.fork()
const result = await branch.exec.run({
cmd: ['python3', 'train.py'],
env: { LR: lr, EPOCHS: epochs },
timeout_ms: 120_000,
})
await branch.delete()
return { lr, output: result.stdout, ok: result.ok }
}),
)
await sandbox.delete()import asyncio
from iris import AsyncIrisClient
async def main():
client = AsyncIrisClient()
sandbox = await client.sandboxes.create()
await sandbox.exec.run(["bash", "-c", "pip install numpy"])
configs = [
{"lr": "0.001", "epochs": "10"},
{"lr": "0.01", "epochs": "10"},
{"lr": "0.1", "epochs": "10"},
]
async def run_config(cfg):
branch = await sandbox.fork()
result = await branch.exec.run(
["python3", "train.py"],
env={"LR": cfg["lr"], "EPOCHS": cfg["epochs"]},
timeout_ms=120_000,
)
await branch.delete()
return {"lr": cfg["lr"], "output": result.stdout, "ok": result.ok}
results = await asyncio.gather(*[run_config(c) for c in configs])
await sandbox.delete()
asyncio.run(main())Checkpoint after setup, fork for each run
const sandbox = await Sandbox.create()
await sandbox.exec.run('pip install -r requirements.txt')
await sandbox.exec.run('python3 seed_data.py')
const cp = await sandbox.checkpoint.create({ name: 'ready' })
for (const input of inputs) {
const run = await sandbox.fork()
await run.files.write('/tmp/input.json', JSON.stringify(input))
const result = await run.exec.run('python3 process.py /tmp/input.json')
console.log(input.id, result.stdout)
await run.delete()
}
await sandbox.delete()import json
sandbox = client.sandboxes.create()
sandbox.exec.run(["bash", "-c", "pip install -r requirements.txt"])
sandbox.exec.run(["python3", "seed_data.py"])
cp = sandbox.checkpoint.create(name="ready")
for input_data in inputs:
run = sandbox.fork()
run.files.write("/tmp/input.json", json.dumps(input_data))
result = run.exec.run(["python3", "process.py", "/tmp/input.json"])
print(input_data["id"], result.stdout)
run.delete()
sandbox.delete()List and manage sandboxes
import { IrisClient } from '@iris/sdk'
const client = new IrisClient()
const sandboxes = await client.sandboxes.list()
for (const sb of sandboxes) {
console.log(sb.id, sb.name, sb.state)
}
await client.sandboxes.suspend(sandboxes[0].id)
await client.sandboxes.resume(sandboxes[0].id)from iris import IrisClient
client = IrisClient()
sandboxes = client.sandboxes.list()
for sb in sandboxes:
print(sb.id, sb.name, sb.state)
client.sandboxes.suspend(sandboxes[0].id)
client.sandboxes.resume(sandboxes[0].id)Run a persistent background service
const sandbox = await Sandbox.create()
await sandbox.services.upsert('redis', {
cmd: 'redis-server',
args: ['--port', '6379'],
})
await sandbox.services.start('redis')
const result = await sandbox.exec.run('redis-cli ping')
console.log(result.stdout) // "PONG\n"
await sandbox.delete()sandbox = client.sandboxes.create()
sandbox.services.upsert("redis", "redis-server", args=["--port", "6379"])
sandbox.services.start("redis")
result = sandbox.exec.run(["redis-cli", "ping"])
print(result.stdout) # "PONG\n"
sandbox.delete()Use the sandbox KV store
const sandbox = await Sandbox.create()
await sandbox.store.set('progress', { value: { step: 1, score: 0.82 } })
const entry = await sandbox.store.get('progress')
console.log('Step:', entry.value.step)
await sandbox.delete()sandbox = client.sandboxes.create()
sandbox.store.set("progress", {"step": 1, "score": 0.82})
entry = sandbox.store.get("progress")
print("Step:", entry.value["step"])
sandbox.delete()Checkpoint and restore in-place
const sandbox = await Sandbox.create()
await sandbox.exec.run('pip install numpy')
const cp = await sandbox.checkpoint.create({ name: 'clean' })
await sandbox.exec.run('pip uninstall -y numpy')
const restore = await sandbox.checkpoint.restore(cp.checkpoint_id)
console.log(`Restored in ${restore.restore_duration_ms}ms`)
const check = await sandbox.exec.run('python3 -c "import numpy; print(numpy.__version__)"')
console.log(check.stdout)
await sandbox.delete()sandbox = client.sandboxes.create()
sandbox.exec.run(["bash", "-c", "pip install numpy"])
cp = sandbox.checkpoint.create(name="clean")
sandbox.exec.run(["bash", "-c", "pip uninstall -y numpy"])
restore = sandbox.checkpoint.restore(cp.checkpoint_id)
print(f"Restored in {restore.restore_duration_ms}ms")
check = sandbox.exec.run(["python3", "-c", "import numpy; print(numpy.__version__)"])
print(check.stdout)
sandbox.delete()