章大侠,发错人了哦,这个不是我咨询的
"Yichun
Zhang (agentzh)" <age...@gmail.com>
发件人: openresty@googlegroups.com
2015-03-10 14:36
请答复 给
openresty@googlegroups.com |
|
收件人
| openresty
<openresty@googlegroups.com>
|
抄送
|
|
主题
| Re:
[openresty] 关于openresty的全局初始化问题 |
|
Hello!
2015-03-09 19:47 GMT-07:00 hxj137:
> 我错误的认为 init_by_lua 是针对单次http req 的,是因为
我在 init_by_lua 中定义了一个全局变量
> 例如 varstatic = 3
>
> 然后在某个请求中
> content_by_lua '
> varstatic = varstatic +1
> ngx.say("varstatic ",varstatic )
> ';
>
> 然后我怎么刷新 值都会是4
注意 nginx 一般会配置多个 worker 进程。各个 worker 进程都有自己独立的
Lua VM 实例。init_by_lua
只是运行在 worker 进程 fork 之前的 master 进程里面的 Lua
VM 中而已。所以 init_by_lua 里面创建的
Lua 全局变量对各个 worker 进程而言只有只读的价值。
如果你要在所有 worker 进程间共享数据,应该使用 lua_shared_dict.
进一步地,应该尽量避免使用 Lua 全局变量,见这里的官方文档中的讨论:
https://github.com/openresty/lua-nginx-module#lua-variable-scope
init_worker_by_lua 配合 ngx.timer.at() 和共享内存字典也可以做复杂的初始化工作,只是要小心处理
race
condition 的问题,即在 timer 完成初始化前,worker 可能已经开始处理请求。
Regards,
-agentzh
--
--------------------------------------------------------
ZTE Information Security Notice: The information contained in this mail (and any attachment transmitted herewith) is privileged and confidential and is intended for the exclusive use of the addressee(s). If you are not an intended recipient, any disclosure, reproduction, distribution or other dissemination or use of the information contained is strictly prohibited. If you have received this mail in error, please delete it and notify us immediately.
--------------------------------------------------------
ZTE Information Security Notice: The information contained in this mail (and any attachment transmitted herewith) is privileged and confidential and is intended for the exclusive use of the addressee(s). If you are not an intended recipient, any disclosure, reproduction, distribution or other dissemination or use of the information contained is strictly prohibited. If you have received this mail in error, please delete it and notify us immediately.