How to beginner · 3 min read

How to run long-running tasks in E2B

Quick answer
Use the Sandbox class from the e2b_code_interpreter package to run long-running tasks by invoking sandbox.run_code() with your code string. For asynchronous or extended tasks, run code in a separate thread or process and manage the sandbox lifecycle carefully to avoid blocking.

PREREQUISITES

  • Python 3.8+
  • pip install e2b-code-interpreter
  • E2B API key set in environment variable <code>E2B_API_KEY</code>

Setup

Install the e2b-code-interpreter package and set your API key in the environment variable E2B_API_KEY. This package provides the Sandbox class to run code securely.

bash
pip install e2b-code-interpreter
output
Collecting e2b-code-interpreter
  Downloading e2b_code_interpreter-1.0.0-py3-none-any.whl (15 kB)
Installing collected packages: e2b-code-interpreter
Successfully installed e2b-code-interpreter-1.0.0

Step by step

Use the Sandbox class to run long-running tasks by calling run_code() with your Python code as a string. Manage the sandbox lifecycle and close it after completion.

python
import os
from e2b_code_interpreter import Sandbox
import time

# Initialize sandbox with API key from environment
sandbox = Sandbox(api_key=os.environ["E2B_API_KEY"])

# Define a long-running Python code snippet
code = '''
import time
for i in range(5):
    print(f"Step {i+1}/5")
    time.sleep(1)
'''

# Run the code in the sandbox
execution = sandbox.run_code(code)

# Print the output
print("Output:")
print(execution.text)

# Close the sandbox to free resources
sandbox.close()
output
Output:
Step 1/5
Step 2/5
Step 3/5
Step 4/5
Step 5/5

Common variations

For asynchronous execution of long-running tasks, run sandbox.run_code() in a separate thread or process to avoid blocking your main program. You can also upload files to the sandbox before running code or install packages dynamically.

python
import os
from e2b_code_interpreter import Sandbox
import threading

sandbox = Sandbox(api_key=os.environ["E2B_API_KEY"])

code = '''
import time
for i in range(3):
    print(f"Async step {i+1}/3")
    time.sleep(2)
'''

def run_long_task():
    result = sandbox.run_code(code)
    print("Async output:")
    print(result.text)
    sandbox.close()

# Run the long task in a separate thread
thread = threading.Thread(target=run_long_task)
thread.start()

print("Main program continues while task runs asynchronously.")

thread.join()
output
Main program continues while task runs asynchronously.
Async output:
Async step 1/3
Async step 2/3
Async step 3/3

Troubleshooting

  • If sandbox.run_code() hangs or times out, ensure your code does not contain infinite loops or blocking calls without timeouts.
  • Always call sandbox.close() to release resources; otherwise, you may hit connection limits.
  • If you need to run multiple long tasks concurrently, create separate Sandbox instances per task.

Key Takeaways

  • Use Sandbox.run_code() to execute long-running Python code safely in E2B.
  • Run long tasks asynchronously with threads or processes to avoid blocking your main program.
  • Always close the sandbox with sandbox.close() to free resources after execution.
Verified 2026-04
Verify ↗