Skip to content

Instantly share code, notes, and snippets.

@moteus
Last active July 14, 2016 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moteus/fc8324c12770f266666b to your computer and use it in GitHub Desktop.
Save moteus/fc8324c12770f266666b to your computer and use it in GitHub Desktop.
Test zmq poll with inproc transport using libuv
ZMQ Version: 4 2 0
Timer recv: hello #1
Timer recv: hello #2
Timer recv: hello #3
Timer recv: hello #4
No messages ([ZMQ][EAGAIN] Resource temporarily unavailable (11))
Poll recv: hello #5
Poll recv: hello #6
Poll recv: hello #7
Poll recv: hello #8
Poll recv: hello #9
local uv = require "lluv"
local zmq = require "lzmq"
print("ZMQ Version:", zmq.version(true))
-- Try set timeout e.g. to 5000
local MAGIC_TIMEOUT = 5000
local ep = "inproc://hello"
local ctx = zmq.context()
local srv = ctx:socket{"PUB", bind = ep }
local cli = ctx:socket{"SUB", connect = ep, subscribe = "" }
local counter = 1
uv.timer():start(1000, 1000, function()
srv:send("hello #" .. counter)
counter = counter + 1
end)
-- `Magic` timer. After it fire all statrs work.
uv.timer():start(MAGIC_TIMEOUT, function()
-- this is enouth
if not cli:has_event(zmq.POLLIN) then
return print("Timer no messages")
end
-- but if we have messages we have read all of them
while true do
local msg, err = cli:recvx(zmq.DONTWAIT)
if not msg then return print("No messages (" .. tostring(err) .. ")" ) end
print("Timer recv:", msg)
end
end)
uv.poll_socket(cli:fd()):start(function(handle, err, event)
if err then
print("Poll error: ", err)
return uv.stop()
end
while cli:has_event(zmq.POLLIN) do
local msg, err = cli:recvx()
if not msg then
print("Recv error : ", err)
return
end
print("Poll recv:", msg)
end
end)
uv.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment