模块 luv

require "luv" — libuv 异步事件循环绑定(后端 luv.dll / libluv.so,来自 luvit 的 luv)

local luv = require "luv"
print(luv.version_string())   -- "1.48.0"

事件循环

API说明返回
luv.run()运行默认事件循环直到无活动句柄bool
luv.loop_alive()事件循环是否仍有活动引用bool
luv.stop()停止事件循环-
luv.now()当前时间戳(ms,自 loop 启动起)num
luv.walk(cb)遍历 loop 中全部句柄,回调 handle-

Timer / 句柄

API说明返回
luv.new_timer()新建定时器句柄timer
timer:start(timeout, repeat, cb)启动:timeout 后首次触发,repeat 周期循环bool
timer:stop()停止定时器bool
timer:again()按原参数重启定时器bool
handle:close([cb])关闭句柄(所有 luv 句柄通用)bool
handle:is_active()句柄是否活动bool
handle:is_closing()句柄是否关闭中bool
handle:ref() / unref()引用/取消引用(控制 loop 存活)-

TCP / UDP / Pipe

API说明返回
luv.new_tcp()新建 TCP 句柄tcp
tcp:bind(ip, port)绑定地址bool,err
tcp:listen(backlog, cb)开始监听,回调接受新连接bool,err
tcp:accept([client])接受一个连接bool,err
tcp:connect(ip, port, cb)连接远端bool,err
stream:read_start(cb)开始读流(cb(err, data))bool,err
stream:write(data[, cb])写数据到流bool,err
stream:read_stop()停止读流bool
luv.new_udp() / udp:bind / send / recv_startUDP 数据报收发
luv.new_pipe(ipc)命名管道 / 进程 IPCpipe

fs / 进程 / 线程

API说明返回
luv.fs_open(path, flags, mode)打开文件,返回 fdfd,err
luv.fs_read(fd, len, pos)读文件,返回 datadata,err
luv.fs_write(fd, data, pos)写文件n,err
luv.fs_close(fd)关闭文件bool,err
luv.fs_stat / fs_unlink / fs_mkdir / fs_scandir文件系统操作
luv.spawn(options, cb)spawn 子进程(options.file/args/stdio)handle
luv.new_thread(func, ...)启动新线程(函数在独立 Lua 状态运行)thread
luv.new_work(cb[, after])线程池任务work
luv.new_signal() / sig:start(kind, cb)信号处理signal

其它

API说明返回
luv.version() / version_string()libuv 版本号 / 版本字符串num / str
luv.hrtime()高分辨率时间(ns)num
luv.uv_uptime()系统开机时长(秒)num,err
luv.os_homedir() / os_tmpdir()家目录 / 临时目录str,err
luv.cwd() / chdir(path)当前工作目录 / 切换目录str / bool

完整 API 与 luvit 官方 luv 一致:https://github.com/luvit/luv

← 返回模块索引