Logic Machine Forum
LUA timers explanation - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: LUA timers explanation (/showthread.php?tid=5541)



LUA timers explanation - Bitver - 31.07.2024

Hi everyone and especially @admin

Could you please explain how to implement robust multifunctional timer script to handle all "timer logic"?
In examples there are many different options, what makes it so confusing.

For example, there is scheduled script example,  copas,  os.microtime() with localbusConfusedtep(), localbus:loop(), timerfd, resident script with deltatime and maybe more. By the way localbusConfusedtep() and localbus:loop() receives unclear number parameters, what are they?
Almost every of examples do the same things: timers.

So, is it possible to make simple (or not) and efficient timer to rule them all?
Or point to already existing one if there is one, please.


RE: LUA timers explanation - admin - 31.07.2024

Describe your task in more detail. It all depends on what kind of timer is needed and what additional logic is there. Scheduled script can be ok for longer time periods where timer precision does not matter much. timerfd + select provides the best precision but the script becomes more complex.
lb:step() receives a single telegram until a timeout happens while lb:loop(timeout) receives multiple telegrams until the specified timeout expires.


RE: LUA timers explanation - Bitver - 31.07.2024

(31.07.2024, 05:57)admin Wrote: Describe your task in more detail. It all depends on what kind of timer is needed and what additional logic is there. Scheduled script can be ok for longer time periods where timer precision does not matter much. timerfd + select provides the best precision but the script becomes more complex.
lbConfusedtep() receives a single telegram until a timeout happens while lb:loop(timeout) receives multiple telegrams until the specified timeout expires.

1. Am I right if I say that better precision loads CPU more?

2. OK, for example I have an idea to implement rx observable in LM to handle complex logic and it requires step timer. 
Precision is +-1 sec and range is "0 sec - several hours"
As I understand, lb:loop(1) with delta correction for better precision is best option for it?

3. Another example is irrigation system where precision +-5 seconds is possible, but it runs in different day in different times configured dinamically by user.
In this case Scheduled script is better or lb:loop(1) with current time checking is still OK?


RE: LUA timers explanation - admin - 02.08.2024

Using timerfd + select should provide the lowest CPU load because the process is in a sleeping state until a telegrams arrives or the timer expires.

For scheduled timers I don't see why localbus is needed. You can simply read the defined timer values from objects and run the required actions as needed. Using os.sleep() should be enough for such tasks.

Use localbus when timer is tied to a certain user action and a quick reaction is needed.