2017-02-20 59 views
0

我想与Lua解释器一起使用OpenResty。Openresty并发请求

我不能让OpenResty框架处理两个并发的请求到两个单独的端点。我模仿一个请求是由一个长循环中运行做了一些硬算了一笔账:

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!') 

的另一个端点只是立即发送响应。 ngx.say('test2')

我向第一个端点发送请求,然后向第二个端点发送第二个请求。但是,OpenResty被第一个请求阻止,所以几乎同时收到两个响应。

将nginx参数worker_processes 1;设置为较高的数字也无济于事,我希望只有一个工作进程。

什么是正确的方式让OpenResty处理额外的请求,而不是被第一个请求阻止?

+0

您不提供任何代码显示我们如何发送子请求。我假设你使用像ngx.location.capture这样的子请求发送到端点。对于并行运行的子请求,您应该使用https://github.com/openresty/lua-nginx-module#ngxlocationcapture_multi API。 –

+0

我没有使用ngx.location.capture。我使用两个独立的客户端连接到同一台服务器上的两个不同端点。 – JeFf

+0

@JeFi对不起,没有理解你的用例,现在很清楚,看到我的回答如下 –

回答

0
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!') 
+0

在第二个while循环中将'coroutine.yield'改为'coroutine.resume'不会导致解释器继续执行协程递增变量'i'。变量'i'只增加到值2.第二个while循环变成无限。 – JeFf

+0

对不起,您正在Openresty上下文中添加“ngx”。所有API的前缀 –