我已经问过这里了,但我想我也会发这样的帖子:
鉴于这一守则:
local redis = require('resty.redis')
local client = redis:new()
client:connect(host,port)
ngx.thread.spawn(function()
ngx.say(ngx.time(),' ',#client:keys('*'))
end)
ngx.timer.at(2,function()
ngx.say(ngx.time(),' ',#client:keys('*'))
end)我知道这个错误:
---urhcdhw2pqoz---
1611628086 5
2021/01/26 10:28:08 [error] 4902#24159: *4 lua entry thread aborted: runtime error: ...local/Cellar/openresty/1.19.3.1_1/lualib/resty/redis.lua:349: bad request
stack traceback:
coroutine 0:
[C]: in function 'send'
...local/Cellar/openresty/1.19.3.1_1/lualib/resty/redis.lua:349: in function 'keys'
./src/main.lua:20: in function <./src/main.lua:19>, context: ngx.timer因此,线程似乎与redis一起工作,但定时器不起作用。为什么?
发布于 2021-01-26 18:06:59
代码中有两个错误。
https://github.com/openresty/lua-nginx-module#ngxsockettcp
在您的示例中,对cosocket对象的引用存储在client表(client._sock)中。
ngx.print/ngx.say在ngx.timer.*上下文中不可用。
https://github.com/openresty/lua-nginx-module#ngxsay (查看上下文:部分)。
您可以使用ngx.log代替(它写入nginx日志,在nginx.conf中设置error_log stderr debug;以将日志打印到stderr)。下列代码按预期工作:
ngx.timer.at(2, function()
local client = redis:new()
client:connect('127.0.0.1' ,6379)
ngx.log(ngx.DEBUG, #client:keys('*'))
end)https://stackoverflow.com/questions/65895230
复制相似问题