lua,使用sleep的各种方法
来源:互联网 发布:人工智能语音机器人 编辑:程序博客网 时间:2024/05/18 02:10
转自:http://lua-users.org/wiki/SleepFunction
Sleep Function
A common need is to pause (sleep) a program for a certain number of seconds, preferably without busy waiting.
This function to do this without busy waiting does not exist in ANSI C, so it does not exist in stock Lua. However, there are extension libraries and calls to external programs that can do this.
Solution: Busy Wait
local clock = os.clockfunction sleep(n) -- seconds local t0 = clock() while clock() - t0 <= n do endend-- warning: clock can eventually wrap around for sufficiently large n-- (whose value is platform dependent). Even for n == 1, clock() - t0-- might become negative on the second that clock wraps.
Solution: C extension
There is a sleep function in ExtensionProposal. This may call Win32 Sleep or POSIX usleep. Here's a [usleep/sleep C wrapper] example.
The LuaApr binding has an [apr.sleep()] function that works on Windows & UNIX and supports sub-second resolution.
The lalarm library[1] can set an alarm on POSIX.
winapi (Windows only) has a [sleep] function. [github]
If an FFI interface (Alien or c/invoke -- BindingCodeToLua) is available, you can call whichever OS function you have.
Solution: sleep command
function sleep(n) os.execute("sleep " .. tonumber(n))end
Windows does not have such a built-in command. However, there's a sleep in the Windows Server Resource Kit. There is also sleep in Cygwin and MinGW. Also, there is "timeout" utility available in Windows 7
os.execute("timeout " .. tonumber(n)) -- specific to win7 (and probably higher)
Solution: ping or other programs
function sleep(n) if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") endend-- 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)endsleep(0.2)
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)endsleep(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() > ntimeend
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 repeat until os.clock() > ntimeend
- lua,使用sleep的各种方法
- Lua的sleep函数
- Lua的sleep函数
- Lua中实现sleep函数功能的4种方法
- 线程的sleep()方法的简单使用
- yield,sleep,join方法的使用
- lua sleep
- 线程 thread的各种方法 join方法会抛异常 sleep方法会抛异常
- 使用lua++.exe的方法
- Lua元方法的使用
- 线程的sleep方法
- Lua的sleep函数(转)
- [lua]写个简单的Lua拓展-sleep函数
- QT 中使用sleep方法
- netty中使用Thread.sleep()方法的问题
- QT中使用类似sleep等待的方法
- Sleep函数的使用
- sleep 函数的使用
- idea tomcat 怎样出现update classes and resources
- 深入理解 Java 虚拟机--Java 内存模型与线程
- 概率图模型7:推理与流动
- 算法设计与应用基础:第十七周(2)
- (10)高通AP10.4开发者指南——WLAN(2.4 Queue管理)
- lua,使用sleep的各种方法
- 第1.1章 docker之centos6.*
- Visual Studio 2010 单元测试之一---普通单元测试
- ionic emulate实时调试修改
- 分割nohup.out文件
- javascript面向对象编程
- 关于idea 修改jsp文件后不能生效
- mysql之 mysql数据库压力测试工具(mysqlslap)
- HMM学习最佳范例: 前向算法(Forward Algorithm)