Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>DISCLAIMER: I didn't really use ZeroMQ before</p> <p>At first note two things: </p> <ol> <li>Server prints "Receiving..." even if client.pl was not started yet. This means that io watcher callback is called even though nothing was sent to this socket yet</li> <li>In server if you substitute line setting up IO watcher (AE::io) with simple while(1) then server/client work correctly. This implies that IO callback is <em>not</em> fired correctly when we supposedly receive message from client (and this makes it "hang").</li> </ol> <p>I'm pretty sure you are running into issues described in <a href="http://funcptr.net/2012/09/10/zeromq---edge-triggered-notification/" rel="nofollow">http://funcptr.net/2012/09/10/zeromq---edge-triggered-notification/</a>. In short:</p> <ol> <li><p>Even if zmq filehandle is notified about being ready to read it does <em>not</em> mean there is message waiting to be received.</p> <p>Callback should check if this is not "false positive".</p> <pre><code>unless (ZMQ_POLLIN &amp; zmq_getsockopt( $responder, ZMQ_EVENTS ) ) { say "Nothing to recv from socket, skipping"; return; } </code></pre> <p>at the beginning of callback seems to do the trick. But I don't think that is critical here. This problem just makes you call zmq_recv before data is there, so it makes your program more blocking</p></li> <li><p>IO watcher may be only called once even if multiple messages are received. This is described nicely in article linked above in "ZeroMQ uses edge triggered" paragraph. That is why you <em>should</em> loop inside callback until no more messages are there. You can do it in a non-blocking way by:</p> <pre><code>while (my $msg = zmq_recvmsg($responder,ZMQ_DONTWAIT)) { say "Received request: [".zmq_msg_data($msg)."]"; zmq_msg_send('World', $responder); } </code></pre> <p>This loop will process all messages there are pending and leave if there are no more.</p> <p><strong>UPDATE</strong> linked article suggests receiving messages in a loop while getsockopt for ZMQ_EVENTS reports that ZMQ_POLLIN is set. This seems more elegant to me. I didn't test it and don't know if there are practical differences.</p> <p>I have no idea <strong>how</strong> in your scenario multiple messages might be received by server on the same time, but that code change above alone seems to fix the problem. Possibly I don't know enough ZeroMQ. I'd be happy if someone can explain the "why" in this specific case to me.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload