> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 生命周期管理

> 查阅 PPIO Agent 沙箱生命周期的归档说明，了解该版本的资源创建与状态管理，为维护既有运行环境提供参考。

每个沙箱都有一个 **timeout**（“超时时间”）配置，当沙箱的运行时间超过该配置值时，沙箱将自动关闭并释放。

您可以参考如下方式在启动的时候显式指定 timeout 的配置值，如果未指定，则默认值为 5 分钟。当前允许设置的**最大值**为 1 小时。

```js JavaScript & TypeScript icon="js" highlight={5} theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter'

// 创建一个沙箱，并保持运行 60 秒。
const sandbox = await Sandbox.create({
  timeoutMs: 60_000, // 单位为毫秒。
})

await sandbox.kill()
```

```python Python icon="python" highlight={5} theme={null}
from ppio_sandbox.code_interpreter import Sandbox

# 创建一个沙箱，并保持运行 60 秒。
sandbox = Sandbox.create(
  timeout=60,  # 单位为秒。
)

sandbox.kill()
```

## 更新沙箱超时时间

您可以参考如下示例来在沙箱运行过程中更新其 timeout 配置值，更新后，沙箱的 timeout 将重置为最新值（从更新生效后的时间点开始计时）。

> [!WARNING]
>
> 请注意，当前沙箱的最大存活时间为 **1 小时**，不能通过本方法让沙箱运行更长的时间。

```js JavaScript & TypeScript icon="js" theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter'

// 创建一个沙箱，并保持运行 60 秒。
const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

// 更改沙箱超时时间为 30 秒（从方法执行后开始计时）。
await sandbox.setTimeout(30_000)

await sandbox.kill()
```

```python Python icon="python" theme={null}
from ppio_sandbox.code_interpreter import Sandbox

# 创建一个沙箱，并保持运行 60 秒。
sandbox = Sandbox.create(timeout=60)

# 更改沙箱超时时间为 30 秒（从方法执行后开始计时）。
sandbox.set_timeout(30)

sandbox.kill()
```

## 获取沙箱信息

您可以通过如下方法来获取沙箱信息，包括沙箱 ID、沙箱模板 ID、沙箱元数据信息、开始时间和结束时间等。

```js JavaScript & TypeScript icon="js" theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter'

// 创建一个沙箱，并保持运行 60 秒。
const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

// 获取沙箱信息。
const info = await sandbox.getInfo()

console.log(info)

// 输出结果示例：
// {
//     sandboxId: 'iadj11lnd93v43ourpi0c',
//     templateId: 'br263f8awvhrqd7ss1ze',
//     name: 'code-interpreter-v1',
//     metadata: {
//       platform_metadata_member: '416092acae23419bbae0df344fd5fxxx',
//       platform_metadata_owner: '0712072841552xxx',
//       platform_metadata_team: 'team_xxx'
//     },
//     envdVersion: '0.2.0',
//     startedAt: 2025-09-01T06:07:24.202Z,
//     endAt: 2025-09-01T06:08:24.202Z,
//     state: 'running',
//     cpuCount: 2,
//     memoryMB: 1024
// }

await sandbox.kill()
```

```python Python icon="python" theme={null}
from ppio_sandbox.code_interpreter import Sandbox

# 创建一个沙箱，并保持运行 60 秒。
sandbox = Sandbox.create(timeout=60)

# 获取沙箱信息。
info = sandbox.get_info()
print(info)

sandbox.kill()
```

## 关闭沙箱

您可以通过调用 `kill` 方法在沙箱自动超时前的任意时刻关闭沙箱实例。

```js JavaScript & TypeScript icon="js" theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter'

// 创建一个沙箱，并保持运行 60 秒。
const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

// 立即关闭沙箱。
await sandbox.kill()
```

```python Python icon="python" theme={null}
from ppio_sandbox.code_interpreter import Sandbox

# 创建一个沙箱，并保持运行 60 秒。
sandbox = Sandbox.create(timeout=60)

# 立即关闭沙箱。
sandbox.kill()
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.