Openresty 并发请求

我想使用带有 Lua 解释器的 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 处理额外的请求并避免被第一个请求阻塞的正确方法是什么?

点赞
用户2060502
用户2060502
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!')

本地函数 busyWaiting() 用于模拟忙等待状态,将当前 coroutine 置于挂起状态。线程 thread 则启动 busyWaiting() 函数。当线程状态不为 'dead' 时,通过 ngx.coroutine.resume(thread) 恢复线程 thread 的执行直至线程结束。最后,通过 ngx.say() 输出信息 'test1!'。

2017-02-22 08:38:46