forked from zerocore-ai/microsandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.py
More file actions
108 lines (83 loc) · 3.13 KB
/
repl.py
File metadata and controls
108 lines (83 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
"""
Advanced example demonstrating the Python sandbox features.
This example shows:
1. Different ways to create and manage sandboxes
2. Resource configuration (memory, CPU)
3. Error handling
4. Multiple code execution patterns
5. Output handling
6. Timeouts and handling long-running starts
Before running this example:
1. Install the package: pip install -e .
2. Start the Microsandbox server (microsandbox-server)
3. Run this script: python -m examples.python_sandbox
Note: If authentication is enabled on the server, set MSB_API_KEY in your environment.
"""
import asyncio
import aiohttp
from microsandbox import PythonSandbox
async def example_context_manager():
"""Example using the async context manager pattern."""
print("\n=== Context Manager Example ===")
async with PythonSandbox.create(name="sandbox-cm") as sandbox:
# Run some computation
code = """
print("Hello, world!")
"""
execution = await sandbox.run(code)
output = await execution.output()
print("Output:", output)
async def example_explicit_lifecycle():
"""Example using explicit lifecycle management."""
print("\n=== Explicit Lifecycle Example ===")
# Create sandbox with custom configuration
sandbox = PythonSandbox(
server_url="http://127.0.0.1:5555", name="sandbox-explicit"
)
# Create HTTP session
sandbox._session = aiohttp.ClientSession()
try:
# Start with resource constraints
await sandbox.start(
memory=1024, # 1GB RAM
cpus=2.0, # 2 CPU cores
)
# Run multiple code blocks with variable assignments
await sandbox.run("x = 42")
await sandbox.run("y = [i**2 for i in range(10)]")
execution3 = await sandbox.run("print(f'x = {x}')\nprint(f'y = {y}')")
print("Output:", await execution3.output())
# Demonstrate error handling
try:
error_execution = await sandbox.run(
"1/0"
) # This will raise a ZeroDivisionError
print("Error:", await error_execution.error())
except RuntimeError as e:
print("Caught error:", e)
finally:
# Cleanup
await sandbox.stop()
await sandbox._session.close()
async def example_execution_chaining():
"""Example demonstrating execution chaining with variables."""
print("\n=== Execution Chaining Example ===")
async with PythonSandbox.create(name="sandbox-chain") as sandbox:
# Execute a sequence of related code blocks
await sandbox.run("name = 'Python'")
await sandbox.run("import sys")
await sandbox.run("version = sys.version")
exec = await sandbox.run("print(f'Hello from {name} {version}!')")
# Only get output from the final execution
print("Output:", await exec.output())
async def main():
"""Run all examples."""
try:
await example_context_manager()
await example_explicit_lifecycle()
await example_execution_chaining()
except Exception as e:
print(f"Error running examples: {e}")
if __name__ == "__main__":
asyncio.run(main())