Closed Phrogz closed 8 years ago
You might want to note that you need libssl-dev installed in order to build cqueues.
I'm not sure if that belongs here? That should be an issue for the cqueues project/rockspec...
I run into this: Error: Unknown protocol git+https
That's a symptom of an old version of luarocks. You should try upgrading to >2.2.0 This wouldn't be an issue if I made an actual release; it's just an issue for using git HEAD with an old luarocks.
@Phrogz did you get this figured out?
@daurnimator I did, thank you. FWIW I ended up bailing and having an engineer write me a Lua binding using hiredis because I needed non-blocking subscriptions for multiple different signatures.
because I needed non-blocking subscriptions for multiple different signatures.
I'm not sure what you mean by this?
lredis allows you to call subscribe
and/or psubscribe
as many times as you desire.
lredis allows you to call subscribe and/or psubscribe as many times as you desire.
Example:
local r=require "lredis.cqueues".connect_tcp()
r:subscribe("foo")
r:psubscribe("b*")
for item in r.get_next, r do
local message_type = item[1]
if message_type == "message" then
print("Channel:", item[2], "Message:", item[3])
elseif message_type == "pmessage" then
print("Channel:", item[3], "Message:", item[4])
end
end
lredis allows you to call
subscribe
and/orpsubscribe
as many times as you desire.
Does it? I don't have the code I tried at the moment (they're at work), but what I ran into, IIRC, was that a call to wait for a callback from a subscribe was synchronous. I needed to write code like the following (using totally made up methods):
local client = require'lredis'
client:connect'localhost'
client:subscribe( "foo.*", function(evt,data)
print( "Got "..evt.." with some data: "..tostring(data) )
-- JavaScript-style setInterval() that publishes new events
-- at 60Hz for 3 seconds
end )
client:subscribe( "bar.*", function(evt,data)
print( "Handling "..evt.." differently" )
client:publish("some.response", data*42 )
end )
client:subscribe( "quit", function() client:close() end )
client:continueHandlingEventsUntilClosed()
As a suggestion: your library seems to fill a nice void, but is sorely missing in documentation and sample code.
Does it? I don't have the code I tried at the moment (they're at work), but what I ran into, IIRC, was that a call to wait for a callback from a subscribe was synchronous.
All lredis functions become asynchronous when run inside a cqueues managed coroutine. They "fake" being blocking when run outside a cqueues coroutine.
local lrc = require "lredis.cqueues"
local cqueues = require "cqueues"
-- Make a new cqueues scheduler
local cq = cqueues.new()
-- Make a thread that prints published messages to stdout
cq:wrap(function()
local r = lrc.connect_tcp()
r:subscribe("foo")
r:psubscribe("b*")
for item in r.get_next, r do
local message_type = item[1]
if message_type == "message" then
print("Channel:", item[2], "Message:", item[3])
elseif message_type == "pmessage" then
print("Channel:", item[3], "Message:", item[4])
end
end
end)
-- Make a second thread that publishes events on an interval
cq:wrap(function()
local r = lrc.connect_tcp()
for i=1, 10000 do
cqueues.sleep(1)
r:call("publish", "bar", tostring(i))
end
end)
-- Start 'main' loop
assert(cq:loop())
Thanks for the example code. I can see now how I might be able to make an event loop that handled read only. The problem comes when I'm not sure if a new get_next
message will come in before I need to asynchronously publish a new event. So I can't write an event loop that could block forever waiting for the next incoming response, I need to be able to time out if no event comes in.
Edit: That cqueues looks wonderful. Hadn't seen it. I'll use what you've given me here soon to see if I can move away from the custom-embedded solution we're using on one side, and the Python scripts we're using on the other.
That cqueues looks wonderful. Hadn't seen it.
Glad to help out. cqueues is an underappreciated library! You may be interested in https://github.com/wahern/cqueues/wiki/Integrations-with-other-main-loops if you're doing this inside another program.
I'll use what you've given me here soon to see if I can move away from the custom-embedded solution we're using on one side, and the Python scripts we're using on the other.
let me know how you go :)
As a suggestion: your library seems to fill a nice void, but is sorely missing in documentation and sample code.
Indeed! It definitely needs some love. I just haven't had the time to take it beyond the 'works for me' stage. If you are using the library, please do contribute links, examples and/or docs :)
I just added the code from my above comment (slightly modified) as an example: https://github.com/daurnimator/lredis/blob/master/examples/pubsub.lua
@Phrogz get a chance to try things out?
libssl-dev
installed in order to buildcqueues
.