Skip to content

Latest commit

 

History

History
113 lines (84 loc) · 3.15 KB

page.mdx

File metadata and controls

113 lines (84 loc) · 3.15 KB

Sandbox lifecycle

When you start the sandbox, it stays alive for 5 minutes by default but you can change it by passing the timeout parameter. After the time passes, the sandbox will be automatically shutdown.

import { Sandbox } from '@e2b/code-interpreter'

// Create sandbox with and keep it running for 60 seconds.
// 🚨 Note: The units are milliseconds.
const sandbox = await Sandbox.create({
  timeoutMs: 60_000, // $HighlightLine
})
from e2b_code_interpreter import Sandbox

# Create sandbox with and keep it running for 60 seconds.
# 🚨 Note: The units are seconds.
sandbox = Sandbox(
  timeout=60, # $HighlightLine
)

Change sandbox timeout during runtime

You can change the sandbox timeout when it's running by calling the the setTimeout method in JavaScript or set_timeout method in Python.

When you call the set timeout method, the sandbox timeout will be reset to the new value that you specified.

This can be useful if you want to extend the sandbox lifetime when it's already running. You can for example start with a sandbox with 1 minute timeout and then periodically call set timout every time user interacts with it in your app.

```js import { Sandbox } from '@e2b/code-interpreter'

// Create sandbox with and keep it running for 60 seconds. const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

// Change the sandbox timeout to 30 seconds. // 🚨 The new timeout will be 30 seconds from now. await sandbox.setTimeout(30_000)

```python
from e2b_code_interpreter import Sandbox

# Create sandbox with and keep it running for 60 seconds.
sandbox = Sandbox(timeout=60)

# Change the sandbox timeout to 30 seconds.
# 🚨 The new timeout will be 30 seconds from now.
sandbox.set_timeout(30)

Retrieve sandbox information

You can retrieve sandbox information like sandbox id, template, metadata, started at/end at date by calling the getInfo method in JavaScript or get_info method in Python.

```js import { Sandbox } from '@e2b/code-interpreter'

// Create sandbox with and keep it running for 60 seconds. const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

// Retrieve sandbox information. const info = await sandbox.getInfo()

// Retrieve sandbox expiration date. const expirationDate = info.endAt console.log(expirationDate)


```python
from e2b_code_interpreter import Sandbox

# Create sandbox with and keep it running for 60 seconds.
sandbox = Sandbox(timeout=60)

# Retrieve sandbox expiration date.
expiration_date = sandbox.get_info().end_at
print(expiration_date)

Shutdown sandbox

You can shutdown the sandbox any time even before the timeout is up by calling the kill method.

```js import { Sandbox } from '@e2b/code-interpreter'

// Create sandbox with and keep it running for 60 seconds. const sandbox = await Sandbox.create({ timeoutMs: 60_000 })

// Shutdown the sandbox immediately. await sandbox.kill()

```python
from e2b_code_interpreter import Sandbox

# Create sandbox with and keep it running for 60 seconds.
sandbox = Sandbox(timeout=60)

# Shutdown the sandbox immediately.
sandbox.kill()