Skip to content

Libuv event loop #2396

Open
Open
@PerilousApricot

Description

@PerilousApricot

I'm wondering if there is a way to use libuv in the background. I've tried the following in init_worker_by_file, and it appears that while the timers fire and the thread is started, subsequent requests never get control:

local uv = require('luv')
local ngx = require('ngx')
print("Initializing worker (hopefully)")
local function my_loop_init_timer(premature)
  -- This timer is run in init context and (hopefully) will let us start a
  -- light thread in the worker context
  local function my_loop_thread()
    -- this hopefully persists for the lifetime of the worker
    print("UV thread executing, uv FD is: " .. uv.backend_fd())
    while ngx.worker.exiting() == false do
      -- I know this is busy waiting, but I don't know another way to tell lua-nginx to poll the fd
      while uv.run("nowait") do
        coroutine.yield()
      end
      coroutine.yield()
      uv.sleep(1)
    end
  end
  ngx.thread.spawn(my_loop_thread)
end
print("Main interpreter is running, uv FD is: " .. uv.backend_fd())

ngx.timer.at(0, my_loop_init_timer)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions