Machine Scheduler API¶
The MachineScheduler implements a priority queue-based scheduling mechanism to execute state machine loops at precise intervals.
class
automation.workers.state_machine.MachineScheduler()A simple scheduler for executing tasks (state machine loops) periodically.
It maintains a queue of ready tasks and a heap of scheduled tasks.
run(self)Main scheduler loop.
Executes tasks in the ready queue and moves scheduled tasks to the ready queue when their deadline is reached. Handles sleep intervals to manage CPU usage.
set_last(self)Updates the last execution timestamp.
sleep_elapsed(self, machine)Sleeps for the remaining time until the next scheduled task.
Parameters:
- machine (StateMachine): The machine associated with the next task.
call_soon(self, func)Schedules a function to be called as soon as possible.
Parameters:
- func (callable): The function to execute.
call_later(self, delay, func, machine)Schedules a function to be called after a delay.
Parameters:
- delay (float): Delay in seconds.
- func (callable): The function to execute.
- machine (StateMachine): The associated state machine instance.
stop(self)Stops the scheduler loop.