CZMQ库为nowaitzstr类(zframezstr_recv_nowait())提供了zframe_recv_nowait()选项,但是没有类似zmsg_recv_nowait()的东西。有什么解决办法吗?而不是将zmq_msg_recvZMQ_DONTWAIT标志一起使用。我的代码是:

zmq_pollitem_t items[] = { {sock, 0, ZMQ_POLLIN, 0} };
zmq_poll(items, 1, 10);
/* now receive all pending messages */
while (1) {
    zmsg_t *msg = zmsg_recv(sock); /* this will block after the last message received */
    /* consume message here */
}
/* sending bunch of messages */


我在做异步REQ/REP。发送多个请求后,准备就绪后会收到答复。此代码将阻止我的应用程序。对我来说,做一个zmq_poll,接收一条消息,依此类推也是很丑的……因为到zmq_poll返回时,其他答复已经到达。

最佳答案

将轮询代码放入循环中。对一个套接字使用poll可以。

一个例子可能会有所帮助:

while (1) {
    /* now receive all pending messages */
    zmq_pollitem_t items[] = { {sock, 0, ZMQ_POLLIN, 0} };

    /* this will block for 10msec, ZMQ_POLL_MSEC is for compatibility for v2.2 */
    int rc = zmq_poll(items, 1, 10 * ZMQ_POLL_MSEC );
    if (rc == -1)
      break; // some error occured, check errno...

    if (items [0].revents & ZMQ_POLLIN) {
      /* there's something to receive */
      zmsg_t *msg = zmsg_recv(sock);
    }

    /* sending bunch of messages */
}

关于c - 如何实现zmsg_recv_nowait?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14870251/

10-09 12:28