Task¶
raspbot.utils.task.Task
Non-blocking timed task utility -- the cooperative multitasking primitive used throughout the library.
Task wraps a callable so that it only executes when at least rate seconds have elapsed since
its last execution.
Calling the task more frequently than rate is safe and cheap -- it simply returns without
executing.
Constructor¶
| Parameter | Type | Default | Description |
|---|---|---|---|
fn |
Callable[[float], Any] |
-- | Function to execute. Receives current_time (float) |
rate |
float |
-- | Minimum interval between executions, in seconds |
run_immediately |
bool |
True |
If True, runs on the very first call |
Calling a task¶
Pass the current monotonic time. The task runs only if enough time has elapsed.
Decorator factory¶
Task.every(rate, run_immediately=True)¶
Decorator that wraps a function as a Task in place.
After decoration, task_sensors is a Task instance.
Call it with task_sensors(ct) in the main loop.
Properties and methods¶
rate¶
Read or update the interval between executions (seconds).
reset()¶
Reset the internal timer so the task fires on the very next call.
run_immediately semantics¶
run_immediately |
First execution |
|---|---|
True (default) |
On the first call, regardless of elapsed time |
False |
After the first full rate period has elapsed |