lua-users home
lua-l archive

Re: efficient timers

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Apr 12, 2015 at 12:49 PM, Nagaev Boris <bnagaev@gmail.com> wrote:
> I have seen the following timers in Lua:
>
> * posix.unistd.sleep [1]
> * posix.time.nanosleep [2]
> * posix.curses.napms [3]
> * ngx.sleep [4]
>
> [1] https://luaposix.github.io/luaposix/modules/posix.unistd.html#sleep
> [2] https://luaposix.github.io/luaposix/modules/posix.time.html#nanosleep
> [3] https://luaposix.github.io/luaposix/modules/posix.curses.html#napms
> [4] http://wiki.nginx.org/HttpLuaModule#ngx.sleep
More that I've snipped from here and there:
Clean sleep method using a repeat … until loop, by Peter Odding,
<http://lua-users.org/lists/lua-l/2008-03/msg00213.html>
Large page of methods at <http://lua-users.org/wiki/SleepFunction>.
Suspend/resume a Lua script using coroutines:
<http://stackoverflow.com/questions/6145059/how-to-suspend-resume-processing-of-a-lua-command>
--- Delay for a number of seconds.
-- @param delay Number of seconds
function delay_s(delay)
 delay = delay or 1
 local time_to = os.time() + delay
 while os.time() < time_to do end
end
-- wait for a reply, using a custom timeout (set to 5 seconds here):
-- send HTTP request
strQuery = "GET " .. strVersionUrlUri .. " HTTP/1.1\r\nHost: " ..
strVersionUrlHost .. "\r\n\r\n"
 net.write(nSocket, strQuery)
 t1 = os.time()
 -- read HTTP response
 strReply = ""
 bFirstRead = true
 nTimeoutSec = 5
 repeat
 if bFirstRead == true then
 repeat nSize, strChunk = net.read(nSocket)
 until strChunk ~= "" or os.difftime(os.time(), t1) > nTimeoutSec
 else
 nSize, strChunk = net.read(nSocket)
 end
 strReply = strReply .. strChunk
 bFirstRead = false
 until nSize <= 0
Best regards,
Paul
-- 
[Notice not included in the above original message: The U.S. National
Security Agency neither confirms nor denies that it intercepted this
message.]

AltStyle によって変換されたページ (->オリジナル) /