1

我想将 OpenResty 与 Lua 解释器一起使用。

我无法让 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 处理额外请求而不被第一个请求阻塞的正确方法是什么?

4

1 回答 1

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!')
于 2017-02-22T08:38:46.603 回答