Last Verified: 2025-01-27
Package:
hopx-ai on PyPI
Overview
TheAsyncSandbox 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
AsyncSandboxwhen:- 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
Sandboxwhen:- 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).
Sandbox.create()
Returns: AsyncSandbox - New sandbox instance
Example:
connect
Connect to an existing sandbox by ID (async).
list
List all sandboxes (async).
iter
Lazy async iterator for sandboxes.
Instance Methods
Lifecycle Methods
All lifecycle methods are async versions ofSandbox methods:
await sandbox.get_info() -> SandboxInfoawait sandbox.start() -> Noneawait sandbox.stop() -> Noneawait sandbox.pause() -> Noneawait sandbox.resume() -> Noneawait sandbox.kill() -> None
Execution Methods
run_code
Execute code with rich output capture (async).
run_code_stream
Stream code execution output (async generator).
list_processes
List running processes in sandbox (async).
kill_process
Kill a process by ID (async).
Utility Methods
await sandbox.get_metrics_snapshot() -> Dict[str, Any]await sandbox.refresh_token() -> Noneawait sandbox.set_timeout(seconds) -> None
Properties
All resource properties are async versions:sandbox.files→AsyncFilessandbox.commands→AsyncCommandssandbox.env→AsyncEnvironmentVariablessandbox.cache→AsyncCachesandbox.terminal→AsyncTerminal
Async Context Manager
TheAsyncSandbox class supports async context manager protocol.
Example:
Examples
Example 1: Basic Async Usage
Example 2: Using Async Context Manager
Example 3: Concurrent Sandboxes
Example 4: Streaming Output
Related Classes
- Sandbox - Synchronous version of AsyncSandbox
- AsyncFiles - Async file operations (accessed via
sandbox.files) - AsyncCommands - Async command execution
- AsyncEnvironmentVariables - Async environment variable management
See Also
- Quickstart Guide - Get started with the SDK
- Sandbox Class - Synchronous sandbox class
- Core Concepts - Learn about sandboxes
Next Steps
- Learn about Synchronous Sandbox if you prefer blocking operations
- Explore Code Execution concepts
- Review File Operations for async file management
- CLI Reference - Use HopX from the command line
- Check out API Reference for direct API access

