Hi, all一段代码如下:init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end另外,在content_by_lua中:content_by_lua ' aaa = "hehe"'我的疑问是,为什么在content_by_lua中 修改 init_by_lua中定义的全局变量的值 无效,但是在 common_module 却可以?
我的一个理解是:在 content_by_lua 中的全局变量 会 通过 __index 元方法自动继承init_by_lua里的全局变量的值,但是 他们不指向同一块内存,所以,在content_by_lua中的改动是不会在 请求间 保留的;那么我的疑问就是,为什么common_module中的改动会更改init_by_lua里的全局变量的值?难道是 因为 他们已经指向同一块内存了?在 2017年1月18日星期三 UTC+8下午6:05:50,xw yang写道:Hi, all一段代码如下:init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end另外,在content_by_lua中:content_by_lua ' aaa = "hehe"'我的疑问是,为什么在content_by_lua中 修改 init_by_lua中定义的全局变量的值 无效,但是在 common_module 却可以? --
Hello因为怕滥用全局标量,content 阶段的全局表并不是真的全局表 (rewrite 等也类似)_G = setmetatable({}, { __index = __G }) (__G 才是真的全局表)至于你的疑问,你看这个官方文档就明白了https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker在 2017年1月18日 下午6:09,xw yang <xingw...@gmail.com>写道:我的一个理解是:在 content_by_lua 中的全局变量 会 通过 __index 元方法自动继承init_by_lua里的全局变量的值,但是 他们不指向同一块内存,所以,在content_by_lua中的改动是不会在 请求间 保留的;那么我的疑问就是,为什么common_module中的改动会更改init_by_lua里的全局变量的值?难道是 因为 他们已经指向同一块内存了?在 2017年1月18日星期三 UTC+8下午6:05:50,xw yang写道:Hi, all一段代码如下:init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end另外,在content_by_lua中:content_by_lua ' aaa = "hehe"'我的疑问是,为什么在content_by_lua中 修改 init_by_lua中定义的全局变量的值 无效,但是在 common_module 却可以? -- --
hello, dejiang zhu请问 下面这段代码 是在 哪看到的?
_G = setmetatable({}, { __index = __G }) 你的解释我大概明白,我的疑问是,为什么 common_module中改动可以?
2017-01-18 21:28 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:Hello因为怕滥用全局标量,content 阶段的全局表并不是真的全局表 (rewrite 等也类似)_G = setmetatable({}, { __index = __G }) (__G 才是真的全局表)至于你的疑问,你看这个官方文档就明白了https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker在 2017年1月18日 下午6:09,xw yang <xingw...@gmail.com>写道:我的一个理解是:在 content_by_lua 中的全局变量 会 通过 __index 元方法自动继承init_by_lua里的全局变量的值,但是 他们不指向同一块内存,所以,在content_by_lua中的改动是不会在 请求间 保留的;那么我的疑问就是,为什么common_module中的改动会更改init_by_lua里的全局变量的值?难道是 因为 他们已经指向同一块内存了?在 2017年1月18日星期三 UTC+8下午6:05:50,xw yang写道:Hi, all一段代码如下:init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end另外,在content_by_lua中:content_by_lua ' aaa = "hehe"'我的疑问是,为什么在content_by_lua中 修改 init_by_lua中定义的全局变量的值 无效,但是在 common_module 却可以? -- -- --
hello在 2017年1月18日 下午10:16,yangxw <xingw...@gmail.com>写道:hello, dejiang zhu请问 下面这段代码 是在 哪看到的?这是我翻译了一下,源码在这里:https://github.com/openresty/lua-nginx-module/blob/master/src/ngx_http_lua_rewriteby.c#L259 _G = setmetatable({}, { __index = __G }) 你的解释我大概明白,我的疑问是,为什么 common_module中改动可以?你的问题,在我上一个链接里有答案,你没看? 2017-01-18 21:28 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:Hello因为怕滥用全局标量,content 阶段的全局表并不是真的全局表 (rewrite 等也类似)_G = setmetatable({}, { __index = __G }) (__G 才是真的全局表)至于你的疑问,你看这个官方文档就明白了https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker在 2017年1月18日 下午6:09,xw yang <xingw...@gmail.com>写道:我的一个理解是:在 content_by_lua 中的全局变量 会 通过 __index 元方法自动继承init_by_lua里的全局变量的值,但是 他们不指向同一块内存,所以,在content_by_lua中的改动是不会在 请求间 保留的;那么我的疑问就是,为什么common_module中的改动会更改init_by_lua里的全局变量的值?难道是 因为 他们已经指向同一块内存了?在 2017年1月18日星期三 UTC+8下午6:05:50,xw yang写道:Hi, all一段代码如下:init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end另外,在content_by_lua中:content_by_lua ' aaa = "hehe"'我的疑问是,为什么在content_by_lua中 修改 init_by_lua中定义的全局变量的值 无效,但是在 common_module 却可以? -- -- -- --
上一个我链接我看了,但是和我这里的情况有点不一样;链接里边的情况是,这个变量直接定义在 require 的模块里边;我这里的情况是,变量定义在 init_by_lua 里,但是在 require 的模块里引用的;我的疑问就是,一个定义在 init_by_lua 里边的变量,为什么能被 require 后的模块复写,却不能被 content_by_lua复写?不能被 content_by_lua复写 的原因,我弄明白了;多谢你的解释2017-01-18 22:53 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:hello在 2017年1月18日 下午10:16,yangxw <xingw...@gmail.com>写道:hello, dejiang zhu请问 下面这段代码 是在 哪看到的?这是我翻译了一下,源码在这里:https://github.com/openresty/lua-nginx-module/blob/master/src/ngx_http_lua_rewriteby.c#L259 _G = setmetatable({}, { __index = __G }) 你的解释我大概明白,我的疑问是,为什么 common_module中改动可以?你的问题,在我上一个链接里有答案,你没看? 2017-01-18 21:28 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:Hello因为怕滥用全局标量,content 阶段的全局表并不是真的全局表 (rewrite 等也类似)_G = setmetatable({}, { __index = __G }) (__G 才是真的全局表)至于你的疑问,你看这个官方文档就明白了https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker在 2017年1月18日 下午6:09,xw yang <xingw...@gmail.com>写道:我的一个理解是:在 content_by_lua 中的全局变量 会 通过 __index 元方法自动继承init_by_lua里的全局变量的值,但是 他们不指向同一块内存,所以,在content_by_lua中的改动是不会在 请求间 保留的;那么我的疑问就是,为什么common_module中的改动会更改init_by_lua里的全局变量的值?难道是 因为 他们已经指向同一块内存了?在 2017年1月18日星期三 UTC+8下午6:05:50,xw yang写道:Hi, all一段代码如下:init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end另外,在content_by_lua中:content_by_lua ' aaa = "hehe"'我的疑问是,为什么在content_by_lua中 修改 init_by_lua中定义的全局变量的值 无效,但是在 common_module 却可以? -- -- -- --
hello, dejiang zhu一个定义在 init_by_lua 里边的变量,为什么能被 require 后的模块复写? 这个原因我大概猜想了一下,没看具体代码: require后的模块,里边所有变量的引用,是直接引用到init_by_lua里边的全局变量的;是这样吗?能给出验证的源代码吗?2017-01-19 9:30 GMT+08:00 yangxw <xingw...@gmail.com>:上一个我链接我看了,但是和我这里的情况有点不一样;链接里边的情况是,这个变量直接定义在 require 的模块里边;我这里的情况是,变量定义在 init_by_lua 里,但是在 require 的模块里引用的;我的疑问就是,一个定义在 init_by_lua 里边的变量,为什么能被 require 后的模块复写,却不能被 content_by_lua复写?不能被 content_by_lua复写 的原因,我弄明白了;多谢你的解释2017-01-18 22:53 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:hello在 2017年1月18日 下午10:16,yangxw <xingw...@gmail.com>写道:hello, dejiang zhu请问 下面这段代码 是在 哪看到的?这是我翻译了一下,源码在这里:https://github.com/openresty/lua-nginx-module/blob/master/src/ngx_http_lua_rewriteby.c#L259 _G = setmetatable({}, { __index = __G }) 你的解释我大概明白,我的疑问是,为什么 common_module中改动可以?你的问题,在我上一个链接里有答案,你没看? 2017-01-18 21:28 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:Hello因为怕滥用全局标量,content 阶段的全局表并不是真的全局表 (rewrite 等也类似)_G = setmetatable({}, { __index = __G }) (__G 才是真的全局表)至于你的疑问,你看这个官方文档就明白了https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker在 2017年1月18日 下午6:09,xw yang <xingw...@gmail.com>写道:我的一个理解是:在 content_by_lua 中的全局变量 会 通过 __index 元方法自动继承init_by_lua里的全局变量的值,但是 他们不指向同一块内存,所以,在content_by_lua中的改动是不会在 请求间 保留的;那么我的疑问就是,为什么common_module中的改动会更改init_by_lua里的全局变量的值?难道是 因为 他们已经指向同一块内存了?在 2017年1月18日星期三 UTC+8下午6:05:50,xw yang写道:Hi, all一段代码如下:init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end另外,在content_by_lua中:content_by_lua ' aaa = "hehe"'我的疑问是,为什么在content_by_lua中 修改 init_by_lua中定义的全局变量的值 无效,但是在 common_module 却可以? -- -- -- -- --
Hello你这描述的我都晕了init 阶段跟 content 阶段是不一样的init 的全局表是真的全局表content 是改装过的,这个之前提过这么说,能解释你的疑问么?在 2017年1月19日 上午9:37,yangxw <xingw...@gmail.com>写道:hello, dejiang zhu一个定义在 init_by_lua 里边的变量,为什么能被 require 后的模块复写? 这个原因我大概猜想了一下,没看具体代码: require后的模块,里边所有变量的引用,是直接引用到init_by_lua里边的全局变量的;是这样吗?能给出验证的源代码吗?2017-01-19 9:30 GMT+08:00 yangxw <xingw...@gmail.com>:上一个我链接我看了,但是和我这里的情况有点不一样;链接里边的情况是,这个变量直接定义在 require 的模块里边;我这里的情况是,变量定义在 init_by_lua 里,但是在 require 的模块里引用的;我的疑问就是,一个定义在 init_by_lua 里边的变量,为什么能被 require 后的模块复写,却不能被 content_by_lua复写?不能被 content_by_lua复写 的原因,我弄明白了;多谢你的解释2017-01-18 22:53 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:hello在 2017年1月18日 下午10:16,yangxw <xingw...@gmail.com>写道:hello, dejiang zhu请问 下面这段代码 是在 哪看到的?这是我翻译了一下,源码在这里:https://github.com/openresty/lua-nginx-module/blob/master/src/ngx_http_lua_rewriteby.c#L259 _G = setmetatable({}, { __index = __G }) 你的解释我大概明白,我的疑问是,为什么 common_module中改动可以?你的问题,在我上一个链接里有答案,你没看? 2017-01-18 21:28 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:Hello因为怕滥用全局标量,content 阶段的全局表并不是真的全局表 (rewrite 等也类似)_G = setmetatable({}, { __index = __G }) (__G 才是真的全局表)至于你的疑问,你看这个官方文档就明白了https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker在 2017年1月18日 下午6:09,xw yang <xingw...@gmail.com>写道:我的一个理解是:在 content_by_lua 中的全局变量 会 通过 __index 元方法自动继承init_by_lua里的全局变量的值,但是 他们不指向同一块内存,所以,在content_by_lua中的改动是不会在 请求间 保留的;那么我的疑问就是,为什么common_module中的改动会更改init_by_lua里的全局变量的值?难道是 因为 他们已经指向同一块内存了?在 2017年1月18日星期三 UTC+8下午6:05:50,xw yang写道:Hi, all一段代码如下:init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end另外,在content_by_lua中:content_by_lua ' aaa = "hehe"'我的疑问是,为什么在content_by_lua中 修改 init_by_lua中定义的全局变量的值 无效,但是在 common_module 却可以? -- -- -- -- -- --
其实 主要问题是,这个aaa定义在 init_by_lua,和定义在common_module里边有差别吗?init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end2017-01-19 10:24 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:Hello你这描述的我都晕了init 阶段跟 content 阶段是不一样的init 的全局表是真的全局表content 是改装过的,这个之前提过这么说,能解释你的疑问么?在 2017年1月19日 上午9:37,yangxw <xingw...@gmail.com>写道:hello, dejiang zhu一个定义在 init_by_lua 里边的变量,为什么能被 require 后的模块复写? 这个原因我大概猜想了一下,没看具体代码: require后的模块,里边所有变量的引用,是直接引用到init_by_lua里边的全局变量的;是这样吗?能给出验证的源代码吗?2017-01-19 9:30 GMT+08:00 yangxw <xingw...@gmail.com>:上一个我链接我看了,但是和我这里的情况有点不一样;链接里边的情况是,这个变量直接定义在 require 的模块里边;我这里的情况是,变量定义在 init_by_lua 里,但是在 require 的模块里引用的;我的疑问就是,一个定义在 init_by_lua 里边的变量,为什么能被 require 后的模块复写,却不能被 content_by_lua复写?不能被 content_by_lua复写 的原因,我弄明白了;多谢你的解释2017-01-18 22:53 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:hello在 2017年1月18日 下午10:16,yangxw <xingw...@gmail.com>写道:hello, dejiang zhu请问 下面这段代码 是在 哪看到的?这是我翻译了一下,源码在这里:https://github.com/openresty/lua-nginx-module/blob/master/src/ngx_http_lua_rewriteby.c#L259 _G = setmetatable({}, { __index = __G }) 你的解释我大概明白,我的疑问是,为什么 common_module中改动可以?你的问题,在我上一个链接里有答案,你没看? 2017-01-18 21:28 GMT+08:00 DeJiang Zhu <douj...@gmail.com>:Hello因为怕滥用全局标量,content 阶段的全局表并不是真的全局表 (rewrite 等也类似)_G = setmetatable({}, { __index = __G }) (__G 才是真的全局表)至于你的疑问,你看这个官方文档就明白了https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker在 2017年1月18日 下午6:09,xw yang <xingw...@gmail.com>写道:我的一个理解是:在 content_by_lua 中的全局变量 会 通过 __index 元方法自动继承init_by_lua里的全局变量的值,但是 他们不指向同一块内存,所以,在content_by_lua中的改动是不会在 请求间 保留的;那么我的疑问就是,为什么common_module中的改动会更改init_by_lua里的全局变量的值?难道是 因为 他们已经指向同一块内存了?在 2017年1月18日星期三 UTC+8下午6:05:50,xw yang写道:Hi, all一段代码如下:init_by_lua ' xxx = require('common_module') aaa = "hello"'在common_module的一个函数中修改 aaalocal function tes() aaa = "hi"end另外,在content_by_lua中:content_by_lua ' aaa = "hehe"'我的疑问是,为什么在content_by_lua中 修改 init_by_lua中定义的全局变量的值 无效,但是在 common_module 却可以? -- -- -- -- -- --