This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Blocking delay
#1
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
Reply
#2
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
Reply
#3
Have a look at this: https://openrb.com/docs/semaphore.htm
Reply
#4
(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
Reply
#5
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.
Reply
#6
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
Reply


Forum Jump: