Skip to content

Integrations with other main loops

daurnimator edited this page Mar 4, 2018 · 13 revisions

Using cqueues libraries

Prosody trunk has a module net.cqueues that implements a cq object you can attach threads to.

Using cqueues as your main loop

There are a few net.server_cqueues implementations around; none have been merged into prosody itself yet.

luv (and hence luvit)

Using cqueues libraries

There is an example located at https://gist.github.com/daurnimator/f1c7965b47a5658b88300403645541aa

Using cqueues as your main loop

There is an example located at https://github.com/luvit/luv/blob/master/examples/cqueues-main.lua

Using cqueues libraries

There is an example located at https://github.com/tarantool/tarantool/pull/1204#issue-121925693

Using cqueues libraries

There is an example located at https://gist.github.com/daurnimator/6874fe358591909e1aa4

Using cqueues libraries

Waiting on https://github.com/openresty/lua-nginx-module/pull/450 to be merged.

Using cqueues libraries

If your main loop is simply a call to socket.select, you can use add a cqueue object like so:

local cqueues = require "cqueues"
local cq = cqueues.new()
local socket = require "socket"

-- Example cqueues thread
cq:wrap(function()
    for i=1, 10 do
        print("SLEEP", i)
        cqueues.sleep(1)
    end
end)

while true do
	local readset, writeset, timeout = {}, {} -- will already exist in your application
    local fake_socket
    if not cq:empty() then
        fake_socket = {getfd = function() return cq:pollfd() end}
        local events = cq:events()
        if events:match"r" then
            table.insert(readset, fake_socket)
        end
        if events:match"w" then
            table.insert(writeset, fake_socket)
        end
        timeout = math.min(cq:timeout() or math.huge, timeout or math.huge)
    end
    local read_ready, write_ready, err = socket.select(readset, writeset, timeout)
    if err == "timeout" or (read_ready and fake_socket and (read_ready[fake_socket] or write_ready[fake_socket])) then
        assert(cq:step(0))
    end
	-- here will be your application's existing socket handling
end

Using cqueues as your main loop

Experimental module at https://gist.github.com/daurnimator/7032fbe5a10a18f47dc5

lgi (GLib)

Using cqueues libraries

Blocked on https://github.com/pavouk/lgi/issues/111

Using cqueues libraries

Example at https://gist.github.com/daurnimator/3842dfdfd3684d297509