Skip to content

Instantly share code, notes, and snippets.

@daurnimator
Last active May 26, 2016 14:09
Show Gist options
  • Save daurnimator/6874fe358591909e1aa4 to your computer and use it in GitHub Desktop.
Save daurnimator/6874fe358591909e1aa4 to your computer and use it in GitHub Desktop.
cqueues running under a lua-ev main loop
-- lua-ev integration
local ev = require "ev"
local cqueues = require "cqueues"
local cq = cqueues.new()
local ev_loop = ev.Loop.new()
local timer, eio
local function step()
local ok, err, errno, thd = cq:step(0)
if not ok then
print("ERROR", debug.traceback(thd, err))
end
local timeout = cq:timeout()
if timeout then
timer:again(ev_loop, timeout)
else
timer:stop(ev_loop)
if cq:empty() then
eio:stop(ev_loop)
end
end
end
timer = ev.Timer.new(step, math.huge)
eio = ev.IO.new(step, cq:pollfd(), ev.READ)
timer:start(ev_loop)
eio:start(ev_loop)
cq:wrap(function()
cqueues.sleep(1.5)
error("this is an error")
end)
cq:wrap(function()
while true do
cqueues.sleep(1)
print("HELLO FROM CQUEUES")
end
end)
ev.Timer.new(function()
print("HELLO FROM EV")
end, 1, 1):start(ev_loop)
ev_loop:loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment