This example demonstrates how to use custom schedulers to control vertex execution order within each superstep.
Schedulers determine which vertices execute first when multiple vertices are ready in parallel. AgentMesh provides three built-in schedulers:
- TopologicalScheduler (default): Lexicographic ordering for deterministic execution
- PriorityScheduler: Priority-based execution (high-priority vertices first)
- ResourceAwareScheduler: Resource-aware scheduling (optimize for memory/CPU)
go run examples/custom_scheduler/main.goThe example runs three parallel vertices with different schedulers:
1. Default TopologicalScheduler:
⚡ Executing: high_priority
⚡ Executing: low_priority
⚡ Executing: medium_priority
(alphabetical order)
2. PriorityScheduler (high-priority first):
⚡ Executing: high_priority
⚡ Executing: medium_priority
⚡ Executing: low_priority
(priority order: 100, 50, 10)
3. ResourceAwareScheduler (low-cost first):
⚡ Executing: low_priority
⚡ Executing: medium_priority
⚡ Executing: high_priority
(cost order: 10, 50, 100)
- ✅ Debugging (reproducible execution order)
- ✅ Testing (consistent results)
- ✅ Simple workflows
- ✅ Critical path optimization (blocking operations first)
- ✅ Cost-based execution (expensive operations early/late)
- ✅ User-defined importance (VIP requests first)
- ✅ Memory-constrained environments (small tasks first)
- ✅ CPU-bound workloads (distribute load evenly)
- ✅ Mixed workload optimization (I/O vs CPU separation)
The example uses a mock graph with three vertices that execute in parallel. By setting MaxWorkers(1), we ensure sequential execution to clearly observe the scheduling order.
priorities := map[string]int{
"critical_node": 100,
"normal_node": 50,
"background": 10,
}
scheduler := pregel.NewPriorityScheduler(priorities, 50)
runtime, _ := pregel.NewRuntime(graph,
pregel.WithScheduler(scheduler),
)// Adjust priorities during execution
scheduler.SetPriority("urgent_task", 200)
priority := scheduler.GetPriority("urgent_task")