Logic Machine Forum
Blocking delay - 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: Blocking delay (/showthread.php?tid=2611)



Blocking delay - benanderson_475 - 24.04.2020

Hi, 

I want to write macro for AV control in lua library so an object can call the function and do a group of things.
when i switch the tv on from off i need to wait 6 sec (time it takes for the tv to start up) before i send it another command 
i don't want to have multiple scripts running so don't want to use os.sleep() what is the best way to do this blocking delay?

Code:
function tv_on()
-- get the state of the tv
local tv_state = get_tv_state() --  returns bool

if tv_state then
-- set to correct input
end

if not tv_state then
-- wait 6 sec then set to correct input
end

end



RE: Blocking delay - AlexLV - 25.04.2020

Hi,
here I see different variants, may be you will find something useful for your case:

http://lua-users.org/wiki/SleepFunction

I think this is good for you:

Solution: os.clock()

. .

Using the os.clock() method instead of os.time(), you can get precision down to one 100th of a second while os.time() only allows intervals based on the timestamp, which at execution can be at anything from 0.1 to 1 second. The os.time() method is great for longer periods over 2 seconds where precision isn't that much of a deal.

function sleep(s)
local ntime = os.clock() + s/10
repeat until os.clock() > ntime
end

BR,

Alex


RE: Blocking delay - admin - 25.04.2020

Have a look at this: https://openrb.com/docs/semaphore.htm


RE: Blocking delay - benanderson_475 - 29.04.2020

(25.04.2020, 04:39)AlexLV Wrote: Hi,
here I see different variants, may be you will find something useful for your case:

http://lua-users.org/wiki/SleepFunction

I think this is good for you:

Solution: os.clock()

. .

Using the os.clock() method instead of os.time(), you can get precision down to one 100th of a second while os.time() only allows intervals based on the timestamp, which at execution can be at anything from 0.1 to 1 second. The os.time() method is great for longer periods over 2 seconds where precision isn't that much of a deal.

function sleep(s)
  local ntime = os.clock() + s/10
  repeat until os.clock() > ntime
end

BR,

Alex

Thanks, i have this working now

function sleep(s)
  local ntime = os.clock() + s
  repeat until os.clock() > ntime
end



RE: Blocking delay - admin - 30.04.2020

Do not use sleep function from the example above. This function consumes all CPU resources but will not block anything. For normal delays use built-in sleep function.

If you need to prevent scripts from running the same code simultaneously then use semaphores.
This example will wait for up to 5 seconds for semaphore named eventlock to be become unlocked. If semaphore is unlocked then dosomething is executed. pcall is needed to catch any execution errors. Otherwise semaphore might remain locked if dosomething fails with an error.
If semaphore is still locked after 5 seconds then no function is executed.

Code:
require('sem')

function dosomething()
  -- do something here
end

stat, err = sem.runlocked('eventlock', 5, function(lockres)
  if lockres then
    return pcall(dosomething)
  else
    return nil, 'lock failed'
  end
end)

if stat then
  log('ok')
else
  log('error', err)
end

This depends on what kind of locking you need. If you just need to some tasks in sequence (queue) then a resident script might be a better solution.


RE: Blocking delay - AlexLV - 30.04.2020

Admin,

thanks, I also noticed that this example I found looks very simple but not good Sad Also will try to use semaphore functionality

BR,

Alex