ZMQ.jl icon indicating copy to clipboard operation
ZMQ.jl copied to clipboard

Set timeout of a socket?

Open malmaud opened this issue 10 years ago • 12 comments

Normally I'd use zmq_setsockopt with rcvtimeo if I only want to wait a fixed amount of time for a socket to receive. The implementation of recvin this wrapper though is blocking when EAGAIN is returned, which seems to defeat the purpose of setting rcvtimo.

malmaud avatar Aug 08 '15 02:08 malmaud

Wouldn't it be more Julian to use a timer task?

stevengj avatar Aug 10 '15 20:08 stevengj

Would you mind elaborating? How would I create a task that is willing to wait up to say 1sec to recv from a socket s?

malmaud avatar Aug 10 '15 20:08 malmaud

Hmm, I guess it is pretty awkward; all the solutions I can come up with involve one task killing another task after a timeout.

stevengj avatar Aug 11 '15 00:08 stevengj

Ya, that was my experience as well.

On Mon, Aug 10, 2015 at 7:46 PM, Steven G. Johnson <[email protected]

wrote:

Hmm, I guess it is pretty awkward; all the solutions I can come up with involve one task killing another task after a timeout.

— Reply to this email directly or view it on GitHub https://github.com/JuliaLang/ZMQ.jl/issues/87#issuecomment-129659072.

malmaud avatar Aug 11 '15 00:08 malmaud

Well i'm not sure this is the most elegant, but something like this would work if you don't mind a blocked task sticking around indefinitely.

s = Socket()
c= Channel()
@async put!((c, :received), recv(s))
@async sleep(1); put!(c, (nothing, :timedout))
data, status = take!(c)

malmaud avatar Aug 14 '15 15:08 malmaud

the blocked task will finish when the Channel is closed, so it shouldn't really be necessary to use the timeout feature in this way. I'm assuming you want this for some form of heartbeat / status indication? For that, I think you can instead create a worker loop that does all of the handling for the channel and have it tickle a local watchdog counter variable whenever it wakes up.

vtjnash avatar Oct 07 '15 16:10 vtjnash

I did end up using a solution along those lines in Requests.jl. I still think it's kinda confusing for a ZMQ wrapper to allow setting rvctimeo but then to not actually timeout, but given that Julia has ways around this it's not a pressing issue for me anymore.

malmaud avatar Oct 07 '15 20:10 malmaud

I agree that it would be nice to timeout if you explicitly requested it. Shouldn't be too hard to implement.

stevengj avatar Oct 07 '15 22:10 stevengj

Jon,

Could you post your solution.

dmiljkovic avatar Nov 04 '15 22:11 dmiljkovic

just what I posted earlier

malmaud avatar Nov 04 '15 23:11 malmaud

thanks

dmiljkovic avatar Nov 04 '15 23:11 dmiljkovic

Once some form of https://github.com/JuliaLang/julia/issues/13763 gets in, it will be an elegant solution to this general kind of situation.

malmaud avatar Nov 05 '15 20:11 malmaud