Branch
Branching launches a specified number of new instances from an instance’s most recent snapshot. This is particularly useful for scenarios where you need multiple identical environments, like parallel testing, training multiple machine learning models, or preparing environments for a team.
If you want to branch the current running state of an instance, create a snapshot first with instance.snapshot(), then branch.
Creating Multiple Clones
Branching an instance creates multiple clone instances based on the instance’s most recent snapshot.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
# (Optional) Capture the current running state before branching
instance.snapshot()
snapshot, clones = instance.branch(count=3)
print(f"Snapshot used for clones: {snapshot.id}")
print("Cloned instances:")
for clone in clones:
print(f" - {clone.id}")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function branchInstance() {
const instance_id = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instance_id: instance_id });
// (Optional) Capture the current running state before branching
await instance.snapshot();
// Create 3 branches from original instance
const level1Result = await instance.branch(3);
// Optional: Create branches from one of the clones
if (level1Result.instances.length > 0) {
const level2Result = await level1Result.instances[0].branch(2);
console.log(`Created ${level2Result.instances.length} level 2 instances`);
}
}
branchInstance();
# Branch an instance to create 3 clones
morphcloud instance branch morphvm_abc123 --count 3
Use Cases for Branching
Branching is particularly useful in the following scenarios:
-
Parallelized Testing: Create multiple identical environments to run different test suites in parallel.
-
Machine Learning Training: Branch a prepared environment to train multiple models with different parameters.
-
Team Development: Create identical environments for each team member from a pre-configured instance.
-
A/B Deployment Testing: Run the same application with different configurations to test performance or behavior.
-
Batch Processing: Process different data chunks in parallel with identical processing environments.