|
24 | 24 | [](https://pyup.io/repos/github/phil65/exxec/) |
25 | 25 |
|
26 | 26 | [Read the documentation!](https://phil65.github.io/exxec/) |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +### Basic Usage |
| 31 | + |
| 32 | +Use the `get_environment()` function to create execution environments: |
| 33 | + |
| 34 | +```python |
| 35 | +from exxec import get_environment |
| 36 | + |
| 37 | +# Local execution (same process) |
| 38 | +env = get_environment("local") |
| 39 | + |
| 40 | +# Subprocess execution (separate process when executing python code) |
| 41 | +env = get_environment("local", isolated=True) |
| 42 | + |
| 43 | +# Docker execution (containerized) |
| 44 | +env = get_environment("docker") |
| 45 | + |
| 46 | +# Execute code |
| 47 | +async with env: |
| 48 | + result = await env.execute(""" |
| 49 | + async def main(): |
| 50 | + return "Hello from execution environment!" |
| 51 | + """) |
| 52 | + print(result.result) # "Hello from execution environment!" |
| 53 | +``` |
| 54 | + |
| 55 | +## Available Providers |
| 56 | + |
| 57 | +### Local Provider |
| 58 | +Executes code in the same Python process. Fastest option but offers no isolation. |
| 59 | + |
| 60 | +```python |
| 61 | +env = get_environment("local", timeout=30.0) |
| 62 | +``` |
| 63 | + |
| 64 | +**Parameters:** |
| 65 | +- `timeout` (float): Execution timeout in seconds (default: 30.0) |
| 66 | +- `isolated` (bool): Whether to execute code in a separate process (default: False) |
| 67 | +- `language` (Language): Programming language (default: "python") |
| 68 | + |
| 69 | + |
| 70 | +### Docker Provider |
| 71 | +Executes code in Docker containers for strong isolation and reproducible environments. |
| 72 | + |
| 73 | +```python |
| 74 | +env = get_environment( |
| 75 | + "docker", |
| 76 | + image="python:3.13-slim", |
| 77 | + timeout=60.0, |
| 78 | + language="python" |
| 79 | +) |
| 80 | +``` |
| 81 | + |
| 82 | +**Parameters:** |
| 83 | +- `lifespan_handler`: Tool server context manager (optional) |
| 84 | +- `image` (str): Docker image to use (default: "python:3.13-slim") |
| 85 | +- `timeout` (float): Execution timeout in seconds (default: 60.0) |
| 86 | +- `language` (Language): Programming language (default: "python") |
| 87 | + |
| 88 | + |
| 89 | +### Daytona Provider |
| 90 | +Executes code in remote Daytona sandboxes for cloud-based development environments. |
| 91 | + |
| 92 | +```python |
| 93 | +env = get_environment( |
| 94 | + "daytona", |
| 95 | + api_url="https://api.daytona.io", |
| 96 | + api_key="your-api-key", |
| 97 | + timeout=300.0, |
| 98 | + keep_alive=False |
| 99 | +) |
| 100 | +``` |
| 101 | + |
| 102 | +**Parameters:** |
| 103 | +- `api_url` (str): Daytona API URL (optional, uses env vars if not provided) |
| 104 | +- `api_key` (str): API key for authentication (optional) |
| 105 | +- `target` (str): Target configuration (optional) |
| 106 | +- `image` (str): Container image (default: "python:3.13-slim") |
| 107 | +- `timeout` (float): Execution timeout in seconds (default: 300.0) |
| 108 | +- `keep_alive` (bool): Keep sandbox running after execution (default: False) |
| 109 | + |
| 110 | + |
| 111 | +### E2B Provider |
| 112 | +Executes code in E2B sandboxes for secure, ephemeral execution environments. |
| 113 | + |
| 114 | +```python |
| 115 | +env = get_environment( |
| 116 | + "e2b", |
| 117 | + template="python", |
| 118 | + timeout=300.0, |
| 119 | + keep_alive=False, |
| 120 | + language="python" |
| 121 | +) |
| 122 | +``` |
| 123 | + |
| 124 | +**Parameters:** |
| 125 | +- `template` (str): E2B template to use (optional) |
| 126 | +- `timeout` (float): Execution timeout in seconds (default: 300.0) |
| 127 | +- `keep_alive` (bool): Keep sandbox running after execution (default: False) |
| 128 | +- `language` (Language): Programming language (default: "python") |
| 129 | + |
| 130 | + |
| 131 | +### Beam Provider |
| 132 | +Executes code in Beam cloud sandboxes for scalable, serverless execution environments. |
| 133 | + |
| 134 | +```python |
| 135 | +env = get_environment( |
| 136 | + "beam", |
| 137 | + cpu=1.0, |
| 138 | + memory=128, |
| 139 | + keep_warm_seconds=600, |
| 140 | + timeout=300.0, |
| 141 | + language="python" |
| 142 | +) |
| 143 | +``` |
| 144 | + |
| 145 | +**Parameters:** |
| 146 | +- `cpu` (float | str): CPU cores allocated to the container (default: 1.0) |
| 147 | +- `memory` (int | str): Memory allocated to the container in MiB (default: 128) |
| 148 | +- `keep_warm_seconds` (int): Seconds to keep sandbox alive, -1 for no timeout (default: 600) |
| 149 | +- `timeout` (float): Execution timeout in seconds (default: 300.0) |
| 150 | +- `language` (Language): Programming language (default: "python") |
| 151 | + |
| 152 | + |
| 153 | +### MCP Provider |
| 154 | +Executes Python code with Model Context Protocol support for AI integrations. |
| 155 | + |
| 156 | +```python |
| 157 | +env = get_environment( |
| 158 | + "mcp", |
| 159 | + dependencies=["requests", "numpy"], |
| 160 | + allow_networking=True, |
| 161 | + timeout=30.0 |
| 162 | +) |
| 163 | +``` |
| 164 | + |
| 165 | +**Parameters:** |
| 166 | +- `dependencies` (list[str]): Python packages to install (optional) |
| 167 | +- `allow_networking` (bool): Allow network access (default: True) |
| 168 | +- `timeout` (float): Execution timeout in seconds (default: 30.0) |
| 169 | + |
| 170 | + |
| 171 | +## Code Execution Patterns |
| 172 | + |
| 173 | +All providers support two execution patterns: |
| 174 | + |
| 175 | +### 1. Main Function Pattern |
| 176 | +```python |
| 177 | +code = """ |
| 178 | +async def main(): |
| 179 | + # Your code here |
| 180 | + return "result" |
| 181 | +""" |
| 182 | +``` |
| 183 | + |
| 184 | +### 2. Result Variable Pattern |
| 185 | +```python |
| 186 | +code = """ |
| 187 | +import math |
| 188 | +_result = math.pi * 2 |
| 189 | +""" |
| 190 | +``` |
| 191 | + |
| 192 | +## Error Handling |
| 193 | + |
| 194 | +Execution results include comprehensive error information: |
| 195 | + |
| 196 | +```python |
| 197 | +async with env: |
| 198 | + result = await env.execute(code) |
| 199 | + if result.success: |
| 200 | + print(f"Result: {result.result}") |
| 201 | + print(f"Duration: {result.duration:.3f}s") |
| 202 | + else: |
| 203 | + print(f"Error: {result.error}") |
| 204 | + print(f"Error Type: {result.error_type}") |
| 205 | +``` |
| 206 | + |
| 207 | +## Multi-Language Support |
| 208 | + |
| 209 | +Some providers support multiple programming languages: |
| 210 | + |
| 211 | +```python |
| 212 | +# JavaScript execution |
| 213 | +env = get_environment("subprocess", language="javascript", executable="node") |
| 214 | + |
| 215 | +# TypeScript execution |
| 216 | +env = get_environment("docker", language="typescript", image="node:18") |
| 217 | +``` |
| 218 | + |
| 219 | +## Advanced Usage |
| 220 | + |
| 221 | +### Context Managers |
| 222 | +All environments are async context managers for proper resource cleanup: |
| 223 | + |
| 224 | +```python |
| 225 | +async with get_environment("docker") as env: |
| 226 | + result1 = await env.execute(code1) |
| 227 | + result2 = await env.execute(code2) # Reuses same container |
| 228 | +# Container automatically cleaned up |
| 229 | +``` |
| 230 | + |
| 231 | +### Custom Configurations |
| 232 | +Each provider supports environment-specific customization: |
| 233 | + |
| 234 | +```python |
| 235 | +# Docker with custom image and networking |
| 236 | +env = get_environment( |
| 237 | + "docker", |
| 238 | + image="tensorflow/tensorflow:latest-py3", |
| 239 | + timeout=600.0 |
| 240 | +) |
| 241 | + |
| 242 | +# Subprocess with specific Python version |
| 243 | +env = get_environment( |
| 244 | + "subprocess", |
| 245 | + executable="/usr/bin/python3.11", |
| 246 | + timeout=120.0 |
| 247 | +) |
| 248 | +``` |
| 249 | + |
| 250 | +### Streaming Output |
| 251 | + |
| 252 | +Some providers support streaming output line by line, useful for long-running processes: |
| 253 | + |
| 254 | +```python |
| 255 | +from exxec import get_environment |
| 256 | + |
| 257 | +# Stream output from subprocess execution |
| 258 | +env = get_environment("subprocess") |
| 259 | + |
| 260 | +async with env: |
| 261 | + async for line in env.execute_stream(""" |
| 262 | + import time |
| 263 | + for i in range(5): |
| 264 | + print(f"Processing step {i+1}...") |
| 265 | + time.sleep(1) |
| 266 | + print("Done!") |
| 267 | + """): |
| 268 | + print(f"Live output: {line}") |
| 269 | + |
| 270 | +# Also works with Docker execution |
| 271 | +env = get_environment("docker") |
| 272 | +async with env: |
| 273 | + async for line in env.execute_stream(code): |
| 274 | + # Process each line as it's produced |
| 275 | + if "ERROR" in line: |
| 276 | + print(f"⚠️ {line}") |
| 277 | + else: |
| 278 | + print(f"✓ {line}") |
| 279 | +``` |
| 280 | + |
| 281 | +**Supported providers:** `docker`, `local`, `beam`, `e2b`, `modal`, `vercel`, `ssh`, `daytona` |
0 commit comments