沙盒实例生命周期管理
支持沙箱的创建、列举、连接、销毁,以及沙箱运行状态检查、信息获取等全生命周期管理操作。
支持函数
| 函数名称 | 说明 |
|---|---|
| sandbox.create(template=..., timeout=...) | 创建沙箱,返回带 sandbox_id 的实例。template必填,timeout_ms可选 |
| sandbox.list(limit=..., next_token=...) | 列举沙箱,返回分页器对象。limit(每页数量)、next_token(分页游标)、qurey(过滤条件)均为可选参数 |
| paginator.next_items() | 获取当前页的沙箱列表,参数均为可选参数;分页器对象还包含 has_next 属性,可判断是否存在下一页数据。 |
| sandbox.kill() | 销毁当前沙箱,立即终止运行并释放资源,调用后沙箱不可再使用 |
| sandbox.connect() | 连接已有沙箱(需传入sandbox_id),暂停的沙箱会自动恢复;opts为可选参数,返回已连接的沙箱 |
| sandbox.is_running() | 检查沙箱是否运行中,opts可选(支持request_timeout,单位:秒),返回布尔值(true=运行中,false=其他状态) |
使用示例
from e2b_code_interpreter import Sandbox
# 创建
sandbox = Sandbox.create(template="sandbox1")
# 可设置自动删除时间,沙盒将在创建完成后,存活设置的时间后,自动删除
# sandbox = Sandbox.create( template="sandbox1",timeout=60)
assert sandbox.sandbox_id
# 列举
paginator = Sandbox.list(limit=100,next_token="next_token")
first_page = paginator.next_items()
# first_page 中每项有 sandbox_id、template_id 等
# 销毁
sandbox.kill()