Logic Machine Forum
How to lock a script during execution? - 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: How to lock a script during execution? (/showthread.php?tid=336)



How to lock a script during execution? - twodbvba - 01.07.2016

Hi,
 
In a project, we use a pushbutton on KNX which triggers a LM4 script that gives instruction to the intercom to give a doorbell sound. But… when they push more than 1 time (like a couples of times directly after eachother), the intercom gets a couple of times the instruction and gets confused.
 
What is the best way to avoid that a running script is executed a second time?
 
Thanks!


RE: How to lock a script during execution? - admin - 01.07.2016

You can use storage to save last script execution time.
This example allows a script to be executed once in 5 seconds, you can tune the timer value as required:
Code:
now = os.time()
prevsend = storage.get('prevsendtime', 0)
delta = now - prevsend

if delta < 0 or delta > 5 then
  storage.set('prevsendtime', now)
  alert('executing script')
end

Another advanced approach is to lock any event script from executing in parallel by using semaphores:
http://openrb.com/docs/semaphore.htm


RE: How to lock a script during execution? - twodbvba - 01.07.2016

Can disabling and enabling the script work? So, at the first line of the script "disable" itself and at the last line "enable" itself?


RE: How to lock a script during execution? - admin - 01.07.2016

That will introduce race condition and it will fail from time to time. The only guaranteed way is to use semaphores.