Runtimes
Runtimes provide scalable compute environments for executing Python code in the Datalayer platform. They offer isolated, managed execution contexts with access to CPU/GPU resources, persistent storage, and environment management capabilities.
Overview
A Runtime represents a remote compute kernel that can execute Python code, manage variables, and handle file operations. Runtimes are created on-demand and can be configured with different environments, resource limits, and execution timeouts.
Key Features
- Isolated Execution: Each runtime provides a clean, isolated Python environment
- Resource Management: Configurable CPU/GPU resources and memory allocation
- Variable Management: Get and set variables across code executions
- File Execution: Execute Python files and scripts remotely
- Snapshot Support: Create snapshots for reproducible environments
- Context Management: Automatic cleanup with Python context managers
Creating Runtimes
Basic Runtime Creation
Create a runtime using a context manager for automatic resource cleanup:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime() as runtime:
response = runtime.execute("print('Hello from Datalayer!')")
print(response.stdout) # Output: Hello from Datalayer!
Advanced Runtime Configuration
Configure runtime with specific environment, name, and timeout:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime(
name="my-ai-runtime",
environment="python-gpu-env", # Use GPU environment
timeout=30 # 30 minute timeout
) as runtime:
# Execute machine learning code
code = """
import numpy as np
import pandas as pd
data = pd.DataFrame({'values': np.random.randn(100)})
print(f"Generated dataset with {len(data)} rows")
"""
response = runtime.execute(code)
print(response.stdout)
Creating Runtime from Snapshot
Restore a runtime from a previously saved snapshot:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime(
name="restored-runtime",
environment="python-cpu-env",
snapshot_name="my-saved-snapshot" # Restore from snapshot
) as runtime:
# Runtime starts with the state from the snapshot
runtime.execute("print('Restored runtime is ready!')")
Code Execution
Execute Code Strings
Run Python code directly as strings:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime() as runtime:
# Execute simple computation
response = runtime.execute("result = 2 + 2")
# Execute more complex code
code = """
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title('Sine Wave')
plt.savefig('sine_wave.png')
print("Plot saved as sine_wave.png")
"""
response = runtime.execute(code)
print(response.stdout)
Execute Python Files
Execute entire Python files remotely:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime() as runtime:
# Execute a local Python file on the remote runtime
response = runtime.execute_file("my_script.py")
print("Script output:", response.stdout)
# Execute with variable input
response = runtime.execute_file(
"data_analysis.py",
variables={"input_file": "data.csv", "output_dir": "/tmp/results"}
)
Execute with Output Variables
Capture specific variables from code execution:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime() as runtime:
# Execute code and return specific variable
result = runtime.execute(
"result = sum([1, 2, 3, 4, 5])",
output="result"
)
print(f"Sum result: {result}") # Output: Sum result: 15
Variable Management
Setting Variables
Set variables in the runtime before code execution:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime() as runtime:
# Set single variable
runtime.set_variable("name", "Alice")
runtime.execute("print(f'Hello, {name}!')")
# Set multiple variables
runtime.set_variables({
"x": 10,
"y": 20,
"operation": "multiply"
})
code = """
if operation == "multiply":
result = x * y
elif operation == "add":
result = x + y
print(f"Result: {result}")
"""
response = runtime.execute(code)
print(response.stdout)
Getting Variables
Retrieve variables from the runtime after execution:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime() as runtime:
# Execute code that creates variables
runtime.execute("""
import pandas as pd
import numpy as np
data = pd.DataFrame({
'A': np.random.randn(5),
'B': np.random.randn(5)
})
mean_A = data['A'].mean()
count = len(data)
""")
# Retrieve variables
data = runtime.get_variable("data")
mean_value = runtime.get_variable("mean_A")
row_count = runtime.get_variable("count")
print(f"Data shape: {data.shape}")
print(f"Mean of column A: {mean_value}")
print(f"Row count: {row_count}")
Snapshot Management
Creating Snapshots
Save runtime state for later restoration:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime(name="ml-runtime") as runtime:
# Set up environment
runtime.execute("""
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
# Create and train model
X = np.random.randn(1000, 10)
y = np.random.randint(0, 2, 1000)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier()
model.fit(X_train, y_train)
print("Model trained successfully")
""")
# Create snapshot without stopping runtime
snapshot = client.create_snapshot(
runtime=runtime,
name="trained-model-snapshot",
description="Runtime with trained RandomForest model",
stop=False
)
print(f"Snapshot created: {snapshot.name}")
# Continue working with the same runtime
runtime.execute("print('Runtime still active after snapshot')")
Alternative Snapshot Creation
Create snapshots directly from runtime:
from datalayer_core import DatalayerClient
client = DatalayerClient()
with client.create_runtime() as runtime:
# Do some work
runtime.execute("""
import torch
model = torch.nn.Linear(10, 1)
print("PyTorch model created")
""")
# Create snapshot using runtime method
snapshot = runtime.create_snapshot(
name="pytorch-model-snapshot",
description="Runtime with PyTorch model",
stop=True # Stop runtime after snapshot
)
print(f"Snapshot saved: {snapshot.name}")
List and Manage Snapshots
from datalayer_core import DatalayerClient
client = DatalayerClient()
# List all snapshots
snapshots = client.list_snapshots()
for snapshot in snapshots:
print(f"Snapshot: {snapshot.name}")
print(f" UID: {snapshot.uid}")
print(f" Description: {snapshot.description}")
print(f" Environment: {snapshot.environment}")
print("---")
# Delete old snapshots
for snapshot in snapshots:
if "old" in snapshot.name:
result = client.delete_snapshot(snapshot)
print(f"Deleted snapshot {snapshot.name}: {result}")
Notes
- Context Managers: Always use
with
statements for automatic resource cleanup - Environment Selection: Choose the appropriate environment based on your computational needs
- Timeout Management: Set realistic timeouts based on expected execution time
- Variable Persistence: Variables persist within a runtime session but are lost when the runtime terminates
- Snapshot Limitations: Snapshots capture the Python environment state but not active network connections
- Resource Billing: Runtime usage is billed based on compute time and resources allocated
- Concurrent Limits: Check your account limits for maximum concurrent runtimes