在MATLAB中开始使用JeroMQ

时间:2016-09-04 11:43:00

标签: matlab zeromq jeromq

我正在尝试使用MATLAB中的JeroMQ,通过实现此示例(How to use jeromq in MATLAB):

% Author : Dheepak Krishnamurthy
% License : BSD 3 Clause

import org.zeromq.ZMQ;

ctx = zmq.Ctx();

socket = ctx.createSocket(ZMQ.REP);

socket.bind('tcp://127.0.0.1:7575');

message = socket.recv(0);

json_data = native2unicode(message.data)';

message = zmq.Msg(8);
message.put(unicode2native('Received'));

socket.send(message, 0);
socket.close()

脚本一直运行到行:

  

message = socket.recv(0);

但卡在那里。

MATLAB将不再响应,必须使用任务管理器杀死。

如果还有其他事情要做,有人可以给出提示,以便让它运行吗?

1 个答案:

答案 0 :(得分:2)

JeroMQ呼叫签名状态:

        /**
         * Receive a message.
         *
         * @return the message received, as an array of bytes; null on error.
         */
        public final byte[] recv()
        {
            return recv(0);
        }

        /**
         * Receive a message.
         *
         * @param flags
         *            the flags to apply to the receive operation.
         * @return the message received, as an array of bytes; null on error.
         */
        public final byte[] recv(int flags)
        {
            zmq.Msg msg = base.recv(flags);

            if (msg != null) {
                return msg.data();
            }

            mayRaise();
            return null;
        }

        /**
         * Receive a message in to a specified buffer.
         *
         * @param buffer
         *            byte[] to copy zmq message payload in to.
         * @param offset
         *            offset in buffer to write data
         * @param len
         *            max bytes to write to buffer.
         *            If len is smaller than the incoming message size,
         *            the message will be truncated.
         * @param flags
         *            the flags to apply to the receive operation.
         * @return the number of bytes read, -1 on error
         */
        public final int recv(byte[] buffer, int offset, int len, int flags)
        {
            zmq.Msg msg = base.recv(flags);

            if (msg != null) {
                return msg.getBytes(0, buffer, offset, len);
            }

            return -1;
        }

您的代码使用第二个:
public final byte[] recv( int flags ){...}

为您的代码为 0 参数注入 flags 的硬编码整数值。

接下来,< int flags 含义在API中更好地记录为:

  

标志 参数是以下定义的标志的组合:
  的 ZMQ_NOBLOCK
  指定应以非阻塞模式执行操作。如果指定套接字上没有可用消息, zmq_recv() 功能将失败, errno 设置为 EAGAIN

如果小心谨慎,可以在DONTWAIT内使用 JeroMQ 标记,但从下到上很难找到其他一些有意义的选项。

最后,让我们阅读ZeroMQ定义的一组 标志

// ------------------------------------------------- // Socket options.                                                           
#define ZMQ_HWM                1
#define ZMQ_SWAP               3
#define ZMQ_AFFINITY           4
#define ZMQ_IDENTITY           5
#define ZMQ_SUBSCRIBE          6
#define ZMQ_UNSUBSCRIBE        7
#define ZMQ_RATE               8
#define ZMQ_RECOVERY_IVL       9
#define ZMQ_MCAST_LOOP        10
#define ZMQ_SNDBUF            11
#define ZMQ_RCVBUF            12
#define ZMQ_RCVMORE           13
#define ZMQ_FD                14
#define ZMQ_EVENTS            15
#define ZMQ_TYPE              16
#define ZMQ_LINGER            17
#define ZMQ_RECONNECT_IVL     18
#define ZMQ_BACKLOG           19
#define ZMQ_RECOVERY_IVL_MSEC 20   /*  opt. recovery time, reconcile in 3.x   */
#define ZMQ_RECONNECT_IVL_MAX 21

// ------------------------------------------------- // Send/recv options.                                                        
#define ZMQ_NOBLOCK 1    // <<<<<<<<<<<<<<<<<<<<<<<<<<- THIS ONE IS NEEDED
#define ZMQ_SNDMORE 2

// ------------------------------------------------- // I/O Multplexing options.
#define ZMQ_POLLIN  1
#define ZMQ_POLLOUT 2
#define ZMQ_POLLERR 4

// ------------------------------------------------- // Device types.
#define ZMQ_STREAMER  1
#define ZMQ_FORWARDER 2
#define ZMQ_QUEUE     3

EPILOGUE:

如果使用.recv( 0 )您的代码将BLOCK ,直到socket - 实例确实收到任何.recv() - 可能在本地方面有任何可能,可能存在于某个近或远的未来,兼容的.connect() - 远程端( REQ 套接字等)。

如果使用.recv( 1 )您的代码将不会阻止,但是应该正确处理两种可能的返回状态 { <msg.data()_Value> | (EXC, NULL) } 并咨询ZeroMQ < strong> errno 详细信息,根据信号情况进行操作。