cortex.core.executor¶
executor ¶
Executor for managing async functions at constant rates.
Provides utilities for executing async callbacks with precise timing, faithful to Python's cooperative multitasking model.
Classes¶
BaseAsyncExecutor ¶
Bases: ABC
Abstract base class for async executors.
Provides common interface for starting, stopping, and running async callback functions.
Source code in src/cortex/core/executor.py
AsyncExecutor ¶
Bases: BaseAsyncExecutor
Runs an async callable in a tight loop, yielding to the event loop.
Used by :class:cortex.core.subscriber.Subscriber to drive its
receive → decode → dispatch loop. Exceptions are logged and the loop
continues; only :class:asyncio.CancelledError stops it.
Example
Source code in src/cortex/core/executor.py
RateExecutor ¶
Bases: BaseAsyncExecutor
Runs an async callable at a target rate in Hz.
Uses time.perf_counter for scheduling. If a callback overruns the
nominal period, next_exec_time stays on the fixed grid (only
+ interval per invocation); the loop then sleeps 0 until the clock
catches up, so missed ticks are not skipped. This matches the
historical neurosim ZMQNODE constant-rate executor behavior and is
appropriate for simulation stepping.
Example
Source code in src/cortex/core/executor.py
SyncRateExecutor ¶
Runs a plain (non-async) callable at a target rate in Hz, on a dedicated thread.
The sync analogue of :class:RateExecutor. Scheduling matches
RateExecutor exactly: a time.perf_counter grid with one
+ interval advance per invocation, so missed ticks are not
skipped when the callback overruns. Pair with
:meth:cortex.core.node.Node.create_timer (mode='sync') for
control-loop work that should not pay the asyncio scheduler tax.
The lifecycle mirrors :class:BaseAsyncExecutor: start /
stop / run / running. stop is thread-safe and sets
an internal :class:threading.Event that the run loop sleeps on,
so a sleeping run loop wakes immediately when another thread calls
:meth:stop — no polling, no shared external event needed.
Example
Source code in src/cortex/core/executor.py
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
Attributes¶
Functions¶
start ¶
Mark the executor as running.
:meth:run calls this automatically; exposed for symmetry with
:meth:BaseAsyncExecutor.start and to make restart-after-stop
safe.
stop ¶
Request the run loop to exit promptly.
Thread-safe. A run loop currently sleeping wakes immediately. Idempotent.
run ¶
Drive func(*args, **kwargs) at rate_hz until :meth:stop.
Blocks the calling thread. *args / **kwargs are forwarded
to self.func on every tick (matches
:meth:BaseAsyncExecutor.run). Exceptions raised by the
callback are logged and the loop continues; only :meth:stop
exits cleanly.