Configuration
Learn how to configure AI Computer for your specific needs.
Basic Configuration
Configure the SandboxClient when initializing:
1from ai_computer import SandboxClient
2
3# Create client with basic configuration
4client = SandboxClient(
5 # Base configuration
6 timeout=30, # Execution timeout in seconds
7 memory_limit="512M" # Memory limit per execution
8)
Resource Limits
Control resource usage in the sandbox environment:
Memory Limits
Specify memory limits using standard size units (M for megabytes, G for gigabytes):
1# Examples of memory limits
2client = SandboxClient(memory_limit="256M") # 256 megabytes
3client = SandboxClient(memory_limit="1G") # 1 gigabyte
Execution Timeout
Set maximum execution time in seconds:
1# Set different timeout values
2client = SandboxClient(timeout=10) # 10 seconds
3client = SandboxClient(timeout=300) # 5 minutes
Example Usage
Here's an example showing how to use configuration options:
1import asyncio
2from ai_computer import SandboxClient
3
4async def run_with_config():
5 # Initialize client with configuration
6 client = SandboxClient(
7 memory_limit="512M",
8 timeout=30
9 )
10
11 # Setup the sandbox
12 await client.setup()
13
14 try:
15 # Run some memory-intensive code
16 code = """
17# Create a large list
18data = list(range(1000000))
19print(f"Created list with {len(data)} items")
20"""
21 response = await client.execute_code(code)
22
23 if response.success:
24 print(response.data['output'])
25 else:
26 print("Execution failed:", response.error)
27
28 finally:
29 await client.cleanup()
30
31asyncio.run(run_with_config())
Best Practices
1. Set Appropriate Limits
Choose resource limits that match your code's requirements without being excessive.
2. Use Timeouts
Always set reasonable timeouts to prevent long-running or stuck processes.
3. Clean Up Resources
Always use try/finally blocks to ensure proper cleanup of sandbox resources.