LUA延时控制

您所在的位置:网站首页 lua循环函数 LUA延时控制

LUA延时控制

2023-11-06 14:53| 来源: 网络整理| 查看: 265

Solution: Busy Wait local clock = os.clock function sleep(n) – seconds local t0 = clock() while clock() - t0 0 then os.execute(“ping -n " … tonumber(n+1) … " localhost > NUL”) end end – version 20100715 - fixed off-by-one second This is mainly for Windows in the absence of a sleep command. Other variations exist, e.g. “perl -e 'sleep(” … tonumber(n) … “)’” or “php -r sleep(” … tonumber(n) … “);”. Solution: I/O wait io.stdin:read’*l’ This is not a sleep but may be useful in similar cases. It waits for the user to press the Enter key. Solution: Using WScript (Windows) function sleep(n) local vb = “test.vbs” local f = assert(io.open(vb,“w”)) f:write(“WScript.Sleep(” … (tonumber(n) * 1000) … “)\n”) f:close() os.execute(vb) end See [2]. Solution: sleep() The POSIX sleep() call provides integer second sleeps. require “posix” posix.sleep(3) Solution: socket.sleep() The LuaSocket? module provides a sleep function. socket = require(“socket”) function sleep(sec) socket.sleep(sec) end sleep(0.2) Solution: ngx.sleep() Nginx Lua module provides a sleep function. One can specify time resolution up to 0.001 seconds (i.e., one milliseconds). Behind the scene, this method makes use of the Nginx timers. ngx.sleep(sec) Solution: lsocket.select() The select() timeout provides a fairly portable sub-second sleep, if you can tolerate the socket library dependency. local lsocket = require(“lsocket”) function sleep(sec) lsocket.select(sec) end sleep(2) Solution: LuaJIT FFI/LuaFFI local ffi = require “ffi” ffi.cdef “unsigned int sleep(unsigned int seconds);” ffi.C.sleep(2) Solution: os.time() function sleep(s) local ntime = os.time() + s repeat until os.time() > ntime end 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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3