Multithreading Openresty并发请求
我想使用OpenResty和Lua解释器 我无法使OpenResty框架处理两个对两个独立端点的并发请求。我模拟了一个请求通过在长循环中运行来进行一些艰难的计算:Multithreading Openresty并发请求,multithreading,nginx,lua,openresty,Multithreading,Nginx,Lua,Openresty,我想使用OpenResty和Lua解释器 我无法使OpenResty框架处理两个对两个独立端点的并发请求。我模拟了一个请求通过在长循环中运行来进行一些艰难的计算: local function busyWaiting() local self = coroutine.running() local i = 1 while i < 9999999 do i = i + 1 coroutine.yield(self) end en
local function busyWaiting()
local self = coroutine.running()
local i = 1
while i < 9999999 do
i = i + 1
coroutine.yield(self)
end
end
local self = coroutine.running()
local thread = ngx.thread.spawn(busyWaiting)
while (coroutine.status(thread) ~= 'zombie') do
coroutine.yield(self)
end
ngx.say('test1!')
本地函数busyWaiting()
本地self=coroutine.running()
局部i=1
而我<9999999
i=i+1
协同程序产量(自我)
结束
结束
本地self=coroutine.running()
本地线程=ngx.thread.spawn(busyWaiting)
而(coroutine.status(thread)~='zombie')呢
协同程序产量(自我)
结束
ngx.say('test1!')
另一个端点只是立即发送响应。
ngx.say('test2')
我向第一个端点发送请求,然后向第二个端点发送第二个请求。但是,OpenResty被第一个请求阻塞,因此我几乎同时收到两个响应
设置nginx参数worker\u进程1代码>到更高的数字也没有帮助,而且我还是希望只有一个工作进程
让OpenResty处理其他请求而不被第一个请求阻止的正确方法是什么?local function busyWaiting()
local function busyWaiting()
local self = ngx.coroutine.running()
local i = 1
while i < 9999999 do
i = i + 1
ngx.coroutine.yield(self)
end
end
local thread = ngx.thread.spawn(busyWaiting)
while (ngx.coroutine.status(thread) ~= 'dead') do
ngx.coroutine.resume(thread)
end
ngx.say('test1!')
本地self=ngx.coroutine.running()
局部i=1
而我<9999999
i=i+1
ngx.协程.产量(自身)
结束
结束
本地线程=ngx.thread.spawn(busyWaiting)
而(ngx.coroutine.status(thread)~='dead')呢
ngx.coroutine.resume(线程)
结束
ngx.say('test1!')
本地函数忙碌等待()
本地self=ngx.coroutine.running()
局部i=1
而我<9999999
i=i+1
ngx.协程.产量(自身)
结束
结束
本地线程=ngx.thread.spawn(busyWaiting)
而(ngx.coroutine.status(thread)~='dead')呢
ngx.coroutine.resume(线程)
结束
ngx.say('test1!')
您没有提供任何代码,说明您如何发送子请求。我假设您使用类似ngx.location.capture的东西向端点发送子请求。您应该为并行运行的子请求使用API。我没有使用ngx.location.capture。我使用两个独立的客户端连接到同一服务器上的两个不同端点。@JeFi抱歉,不了解您的用例,现在很清楚,请参见下面的我的答案您解决过这个问题吗?@MappaM,不幸的是,我没有。您没有提供任何代码说明如何发送子请求。我假设您使用类似ngx.location.capture的东西向端点发送子请求。您应该为并行运行的子请求使用API。我没有使用ngx.location.capture。我使用两个独立的客户端连接到同一服务器上的两个不同端点。@JeFi抱歉,不理解您的用例,现在很清楚,请参见下面的答案。您解决过这个问题吗?@MappaM,不幸的是,我没有这样做。将coroutine.yield
更改为coroutine.resume
以秒为单位,而循环不会导致解释器继续使用coroutine递增变量I
。变量i
仅递增到值2。第二个while循环将变得无限。很抱歉,您正在Openresty上下文中工作,请在所有APIngx.coroutine中添加“ngx”。前缀不存在,并且此答案无法解决问题…@MappaM您很有趣,您应该在回答中解释您更改了什么以及您认为它应该工作的原因。可能是因为我尝试使用了NGINX+Lua的标准版本,而不是Openresty的版本。其中,协程是主包名。ngx.coroutine不存在,并给出一个错误。使用coroutine.yield并不能解决当前的问题,即使在让步时,服务器也不会提供另一个请求。将coroutine.yield
更改为coroutine.resume
,而循环不会导致解释器继续使用coroutine递增变量i
。变量i
仅递增到值2。第二个while循环将变得无限。很抱歉,您正在Openresty上下文中工作,请在所有APIngx.coroutine中添加“ngx”。前缀不存在,并且此答案无法解决问题…@MappaM您很有趣,您应该在回答中解释您更改了什么以及您认为它应该工作的原因。可能是因为我尝试使用了NGINX+Lua的标准版本,而不是Openresty的版本。其中,协程是主包名。ngx.coroutine不存在,并给出一个错误。而且使用coroutine.yield并不能解决当前的问题,即使在让步时,服务器也不会为另一个请求提供服务。