1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
10 small wins to finish your pathNext lesson →
Sandbox lifecycle: templates, timeouts and kill
A sandbox starts from a template, runs until its timeout or until you kill it, and can have its timeout extended. The defaults are in the SDK.
from e2b_code_interpreter import Sandbox
from e2b_code_interpreter.constants import DEFAULT_TIMEOUT
print("template:", Sandbox.default_template)
print("sandbox timeout:", Sandbox.default_sandbox_timeout, "seconds")
print("run_code timeout:", DEFAULT_TIMEOUT, "seconds")The code interpreter's default template is code-interpreter-v1: Linux with a Jupyter kernel and common data libraries. A sandbox lives 300 seconds unless you say otherwise, and a single run_code may run for 300 seconds.
sandbox = Sandbox.create(timeout=60) # alive for 60 seconds
sandbox.set_timeout(120) # now 120 seconds from this call
print(sandbox.sandbox_id)
print(sandbox.get_info())
sandbox.kill()timeoutis in seconds in Python, and milliseconds,timeoutMs, in the JavaScript SDK.set_timeoutrestarts the countdown; an app calls it while a user is still active.sandbox_ididentifies it;Sandbox.connect(sandbox_id)reattaches from another process.killstops it immediately. An expired sandbox is stopped for you, or paused if you configured lifecycle options.
import inspect
from e2b_code_interpreter import Sandbox
for name in ["timeout", "allow_internet_access", "envs", "metadata", "template"]:
print(name, "=", inspect.signature(Sandbox.create).parameters[name].default)These are create's defaults, read from the installed SDK: no custom timeout, internet access on, no environment variables or metadata, the default template. Lesson 7 changes the internet default.
Try it yourself
- Print every parameter of
Sandbox.create. - Find
default_sandbox_timeoutin the SDK's source. - Print the signature of
Sandbox.connect.
You understood something today that you didn't yesterday.