semaphore unlock after timeout - 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: semaphore unlock after timeout (/showthread.php?tid=2415) |
semaphore unlock after timeout - benanderson_475 - 03.01.2020 hi, is there a way i can i have this to unlock itself after the timeout period executed by the callback? many thanks, require('sem') -- wait for 5 seconds max before executing callback res1, res2 = sem.runlocked('eventlock', 5, function(lockres) -- lock acquired if lockres then return true, 'lock ok' -- failed to acquire lock else return nil, 'lock failed' end end) log(res1, res2) RE: semaphore unlock after timeout - admin - 06.01.2020 This is not safe because it leads to race conditions which semaphores prevent. Can you explain why do you need this? If your function can fail due to an error you should execute it using pcall(). RE: semaphore unlock after timeout - benanderson_475 - 06.01.2020 (06.01.2020, 15:18)admin Wrote: This is not safe because it leads to race conditions which semaphores prevent. Can you explain why do you need this? If your function can fail due to an error you should execute it using pcall().Ok thanks, what i am trying to achieve is to have a event script, switch on a group, start a timer then, timeout and switch off a group (also block the resident script from running again while the timer is running ) the event script is triggered by an security alarm message which can happen quite fast (every time the pir is triggered, this is why i want to block the script from running if it has been triggered so i don't have multiple scripts running) are you able to point me in the right direction. Many Thanks RE: semaphore unlock after timeout - apkotelnikov - 06.01.2020 (06.01.2020, 20:55)benanderson_475 Wrote:Hi!(06.01.2020, 15:18)admin Wrote: This is not safe because it leads to race conditions which semaphores prevent. Can you explain why do you need this? If your function can fail due to an error you should execute it using pcall().Ok thanks, Perhaps my solution will be useful. You must create a resident script Code: local my_tmr_table = storage.get('my_timers') Also you need function to set timer Code: function my_timer (my_grp , on_t , dur) my_grp - group name on_t - UNIX time at timer setup dur - duration in seconds Now you can make script for an group address like this Code: if event.getvalue() then Anton RE: semaphore unlock after timeout - benanderson_475 - 07.01.2020 Hi Anton, Many thanks, this is what i am looking for. This works well. |