Skip to main content
Version: 0.3.0
Last Verified: 2025-01-27
Package: hopx-ai on PyPI

Overview

The AsyncSandbox class provides an asynchronous interface for creating and managing cloud sandboxes. Use this class when building async Python applications (FastAPI, aiohttp, etc.) or when you need to manage multiple sandboxes concurrently. All methods in AsyncSandbox are async and must be called with await. The class supports async context managers for automatic cleanup.

When to Use AsyncSandbox vs Sandbox

  • Use AsyncSandbox when:
    • Building async Python applications (FastAPI, aiohttp, etc.)
    • You need to manage multiple sandboxes concurrently
    • You want non-blocking operations
    • You’re already using async/await in your codebase
  • Use Sandbox when:
    • Building synchronous Python applications
    • Writing scripts or simple automation
    • You prefer blocking operations

Import

Class Methods

create

Create a new sandbox from a template (async).
Parameters: Same as Sandbox.create() Returns: AsyncSandbox - New sandbox instance Example:
Expected Output:

connect

Connect to an existing sandbox by ID (async).
Example:
Expected Output:

list

List all sandboxes (async).
Example:
Expected Output:

iter

Lazy async iterator for sandboxes.
Expected Output:

Instance Methods

Lifecycle Methods

All lifecycle methods are async versions of Sandbox methods:
  • await sandbox.get_info() -> SandboxInfo
  • await sandbox.start() -> None
  • await sandbox.stop() -> None
  • await sandbox.pause() -> None
  • await sandbox.resume() -> None
  • await sandbox.kill() -> None

Execution Methods

run_code

Execute code with rich output capture (async).
Example:
Expected Output:

run_code_stream

Stream code execution output (async generator).
Expected Output:

list_processes

List running processes in sandbox (async).
Expected Output:

kill_process

Kill a process by ID (async).
Expected Output:

Utility Methods

  • await sandbox.get_metrics_snapshot() -> Dict[str, Any]
  • await sandbox.refresh_token() -> None
  • await sandbox.set_timeout(seconds) -> None

Properties

All resource properties are async versions:
  • sandbox.filesAsyncFiles
  • sandbox.commandsAsyncCommands
  • sandbox.envAsyncEnvironmentVariables
  • sandbox.cacheAsyncCache
  • sandbox.terminalAsyncTerminal

Async Context Manager

The AsyncSandbox class supports async context manager protocol. Example:
Expected Output:

Examples

Example 1: Basic Async Usage

Expected Output:

Example 2: Using Async Context Manager

Expected Output:

Example 3: Concurrent Sandboxes

Expected Output:

Example 4: Streaming Output

Expected Output:

See Also

Next Steps