0

CZMQ 库为和类(和)提供nowait选项,但没有像. 有什么解决办法吗?而不是与标志一起使用。我的代码是:zstrzframezstr_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返回时,其他回复已经到达。

4

1 回答 1

2

将投票代码放入循环中。对一个套接字使用 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 */
}
于 2013-02-16T06:21:05.687 回答