run_for
result = run_for(duration, [pace=], [tolerance=1 ms])
Input: duration, pace, tolerance · Output: result
What it is
Drives every registered every/after/at timer across a simulated virtual timeline, not a real wall-clock sleep. duration (number — seconds, same convention as every/after/at's own interval arguments) is how far along that virtual timeline to advance. Timers interleave by scheduled fire time, the way a real event loop would. Requires at least one timer to already be registered, or it errors.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = run_for(duration, [pace=], [tolerance=1 ms]) | duration numberduration (number — seconds, same convention as every/after/at's own interval arguments) is how far along that virtual timeline to advance.pace optionalpace is the real-time factor (1 real time, 2 twice as fast, 0.5 half speed): a firing scheduled at virtual time t is held back until t / pace seconds after the call started, and the call returns once duration / pace seconds have passed.tolerance optional (default 1 ms)A step that starts more than tolerance (seconds, default 1 ms) after its deadline is counted as an overrun. | tolerance —seconds, default 1 ms | Returns an integer: the total number of timer firings that occurred while advancing. Paced mode (*added in v0.4.14*): with pace= the same timeline is run against the wall clock. pace is the real-time factor (1 real time, 2 twice as fast, 0.5 half speed): a firing scheduled at virtual time t is held back until t / pace seconds after the call started, and the call returns once duration / pace seconds have passed. Deadlines are anchored to the start, so one late step does not delay the rest; nothing is skipped to catch up. A step that starts more than tolerance (seconds, default 1 ms) after its deadline is counted as an overrun; read the counts with run_for_stats(). This is soft real time on a desktop operating system: no deadline is guaranteed, the wait spins for the last millisecond (one busy core), and an interpreted timer body costs a few microseconds per statement, so loops up to a few kHz are realistic and tens of kHz are not. Without pace= nothing waits and the behaviour is exactly as before. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
count = 0
every 1 s do
count = count + 1
end
run_for(5 s)
print("fired {count} times")
· run_for(5s): 5 timer firing(s) across 1 registered timer(s) fired 5 times
from specialized
How it works
run_for(duration) (seconds — same convention as every/ after/at, e.g. run_for(500 ms) or run_for(0.5)) drives every registered timer across a simulated virtual timeline, not a real wall-clock sleep — a batch script asking to "simulate 10s of a control loop" should not actually block for 10 real seconds (and a real-time sleep would make test suites slow or flaky). Timers interleave by scheduled fire time (not "run timer A to completion, then timer B"), the way a real event loop would. pace= (v0.4.14) is the opt-in real-time mode: the same timeline, but each firing is held back to its wall-clock deadline start + virtual_time / pace and its lateness is and the output and return value are what they always were.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.