ngx.timer.at(0, func)
来源:互联网 发布:linux top 进程状态 编辑:程序博客网 时间:2024/06/05 20:33
ngx.timer.at
syntax: ok, err = ngx.timer.at(delay, callback, user_arg1, user_arg2, …)
The first argument, delay, specifies the delay for the timer, in seconds.
One can specify fractional seconds like 0.001 to mean 1 millisecond here.
0 delay can also be specified, in which case the timer will immediately expire when the current handler yields execution.
Here is a simple example:
location / { ... log_by_lua ' local function push_data(premature, uri, args, status) -- push the data uri, args, and status to the remote -- via ngx.socket.tcp or ngx.socket.udp -- (one may want to buffer the data in Lua a bit to -- save I/O operations) end local ok, err = ngx.timer.at(0, push_data, ngx.var.uri, ngx.var.args, ngx.header.status) if not ok then ngx.log(ngx.ERR, "failed to create timer: ", err) return end ';}
问题是:ngx.timer.at(0, func)和直接调用func有什么区别吗?
When a timer expires, the user Lua code in the timer callback is running in a “light thread” detached completely from the original request creating the timer.
So objects with the same lifetime as the request creating them, like cosockets, cannot be shared between the original request and the timer user callback function.
A lot of the Lua APIs for Nginx are enabled in the context of the timer callbacks, like stream/datagram cosockets (ngx.socket.tcp and ngx.socket.udp), shared memory dictionaries (ngx.shared.DICT), user coroutines (coroutine.), user “light threads” (ngx.thread.), ngx.exit, ngx.now/ngx.time, ngx.md5/ngx.sha1_bin, are all allowed. But the subrequest API (like ngx.location.capture), the ngx.req.* API, the downstream output API (like ngx.say, ngx.print, and ngx.flush) are explicitly disabled in this context.
You can pass most of the standard Lua values (nils, booleans, numbers, strings, tables, closures, file handles, and etc) into the timer callback, either explicitly as user arguments or implicitly as upvalues for the callback closure. There are several exceptions, however: you cannot pass any thread objects returned by coroutine.create and ngx.thread.spawn or any cosocket objects returned by ngx.socket.tcp, ngx.socket.udp, and ngx.req.socket because these objects’ lifetime is bound to the request context creating them while the timer callback is detached from the creating request’s context (by design) and runs in its own (fake) request context. If you try to share the thread or cosocket objects across the boundary of the creating request, then you will get the “no co ctx found” error (for threads) or “bad request” (for cosockets). It is fine, however, to create all these objects inside your timer callback.
- ngx.timer.at(0, func)
- ngx.exit,ngx.eof,ngx.timer.at
- 咋没人提ngx.timer.at这个方法的执行次数呢?
- func
- func
- ngx lua
- virtual Func() const=0
- ngx.print与ngx.say
- Read button status using poll way, also with timer, atomic_t/semaphore, NONBLOCK func inside
- NGX 信号处理流程
- NGX 内存池实现
- NGX --- ACCEPT事件处理
- ngx slab分析
- ngx上传模块
- ngx conf相关
- ngx 全局变量共享内存
- ngx事件驱动机制
- ngx的数据结构理解
- gradle构建代码加速(即AS更快打开其他项目)的方法
- ssh-keygen的使用方法
- 解决MAC电脑下魅族MX4手机adb连接问题
- 【cocos2d-x 报错异常集】运行安卓虚拟机的时候出现了emulator: ERROR: Can't find 'Linux version ' string in kernel image fi
- 换乘费用
- ngx.timer.at(0, func)
- 浅析MySQL中exists与in的使用
- iOS 5 - Apple LLVM compiler 3.0 error
- 聊聊高并发(四十一)解析java.util.concurrent各个组件(十七) 任务的异步执行和状态控制
- HTTP 错误 500.21 - Internal Server Error 解决方案
- 单例模式的七种写法
- 简易电子词典的实现
- LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- MarkdownPad使用