dofile
require
Hello! On Sun, Dec 27, 2015 at 7:45 PM, jakiejia wrote: > 我在nginx.conf中require一个模块,但是在这个模块中使用了ngx.exit函数,不知道这样会不会触发这个错误,目前测试的时候没发现这个问题。 > https://github.com/openresty/lua-nginx-module#lua-coroutine-yieldingresuming > 只要在你的 Lua 模块加载的过程中没有调用 ngx.exit() 就不会有问题。因为 Lua 模块加载时执行的代码是直接由 require() 来发起的。比如下面这个有问题的例子: location = /t { content_by_lua_block { require("foo") } } 这里的 foo 模块文件 foo.lua 是这么实现的: -- foo.lua ngx.exit(403) return {} 访问 /t 时会得到 500 而不是 403,因为下面这个(未捕获的) Lua 异常: 015/12/27 20:46:44 [error] 37491#0: *1 lua entry thread aborted: runtime error: attempt to yield across C-call boundary Regards, -agentzh
Hello! 2015-12-27 21:01 GMT-08:00 jakiejia: > 我理解的意思是在入口函数不调用就可以,对吧,谢谢春哥! > 不是入口函数。比如你那个例子里的入口函数是 main(),在 main() 里调用 ngx.exit() 没问题。只要不是在模块加载代码里调用。所谓加载代码就是顶层作用域里的代码序列,不属于任何模块内定义的函数。 Regards, -agentzh