Scheduling API¶
Parallel execution engine with conflict detection.
Overview¶
The scheduler automatically parallelizes system execution based on declared access patterns: - Conflict detection: Identifies write-write and read-write conflicts - Parallel execution: Groups non-conflicting systems - Query optimization: Leverages query disjointness for more parallelism - Async-first: Uses asyncio.gather for efficient concurrent execution
Execution Modes: - Parallel: Default mode, maximizes throughput - Sequential: For debugging and deterministic execution
SimpleScheduler¶
Parallel execution with conflict detection and query disjointness optimization.
SimpleScheduler
¶
Scheduler with parallel execution.
All systems execute in parallel (snapshot isolation), with results concatenated in registration order and applied at group boundaries.
Execution grouping is delegated to an ExecutionGroupBuilder, enabling future extensions for dependency-based, frequency-based, or custom grouping strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SchedulerConfig | None
|
Scheduler configuration (concurrency, retry). |
None
|
group_builder
|
ExecutionGroupBuilder | None
|
Strategy for building execution groups from systems. Defaults to SingleGroupBuilder (all parallel, dev systems isolated). |
None
|
Source code in src/agentecs/scheduling/scheduler.py
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
__init__(config=None, group_builder=None)
¶
Source code in src/agentecs/scheduling/scheduler.py
tick(world)
¶
tick_async(world)
async
¶
Execute all systems once, parallelizing where possible.
Source code in src/agentecs/scheduling/scheduler.py
SequentialScheduler¶
Simple sequential execution for debugging.
SequentialScheduler()
¶
Create a scheduler that executes systems one at a time.
Equivalent to SimpleScheduler with max_concurrent=1. Useful for debugging or when parallelism isn't needed.
Source code in src/agentecs/scheduling/scheduler.py
Conflict Detection¶
How the scheduler determines if systems can run in parallel:
Write-Write Conflicts¶
Two systems conflict if both write to the same component type.
@system(writes=(Position,))
def move_system(world): ...
@system(writes=(Position,)) # Conflicts with move_system
def teleport_system(world): ...
Read-Write Conflicts¶
A system reading a component conflicts with one writing it.
@system(reads=(Position,), writes=(Velocity,))
def physics_system(world): ...
@system(writes=(Position,)) # Conflicts with physics_system
def move_system(world): ...
Query Disjointness¶
Systems with provably disjoint queries can parallelize even with same component types.
@system(reads=Query().having(Agent, Active))
def active_agents(world): ...
@system(reads=Query().having(Agent, Inactive)) # Disjoint!
def inactive_agents(world): ...
Usage Example¶
from agentecs import World
from agentecs.scheduling import SimpleScheduler, SchedulerConfig
# Create world with scheduler
world = World(execution=SimpleScheduler())
# Or with custom config
world = World(
execution=SimpleScheduler(
config=SchedulerConfig(
max_concurrent=10
)
)
)
# Register systems
world.register_system(movement_system)
world.register_system(physics_system)
world.register_system(rendering_system)
# Execute one tick (parallel)
await world.tick_async()
# Or synchronous wrapper
world.tick()
Future Enhancements¶
Frequency-Based Execution (Planned): - Systems declare execution frequency (every N ticks) - Reduces unnecessary computation - Phase-based grouping
Context-Aware Scheduling (Research): - Optimize for LLM cache hits - Group systems with overlapping context - Learn optimal schedules from execution patterns