-
Notifications
You must be signed in to change notification settings - Fork 37
Integrations with other main loops
Prosody trunk has a module net.cqueues
that implements a cq
object you can attach threads to.
There are a few net.server_cqueues
implementations around; none have been merged into prosody itself yet.
There is an example located at https://gist.github.com/daurnimator/f1c7965b47a5658b88300403645541aa
There is an example located at https://github.com/luvit/luv/blob/master/examples/cqueues-main.lua
There is an example located at https://github.com/tarantool/tarantool/pull/1204#issue-121925693
There is an example located at https://gist.github.com/daurnimator/6874fe358591909e1aa4
nginx's lua-nginx-module (openresty)
Waiting on https://github.com/openresty/lua-nginx-module/pull/450 to be merged.
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
Experimental module at https://gist.github.com/daurnimator/7032fbe5a10a18f47dc5
lgi (GLib)
Blocked on https://github.com/pavouk/lgi/issues/111
Example at https://gist.github.com/daurnimator/3842dfdfd3684d297509