首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MATLAB中开始使用JeroMQ

在MATLAB中开始使用JeroMQ
EN

Stack Overflow用户
提问于 2016-09-04 11:43:00
回答 1查看 2.1K关注 0票数 4

我试着用MATLAB的JeroMQ,

通过实现此示例( How to use jeromq in MATLAB ):

代码语言:javascript
复制
% 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就不再响应了,不得不用任务管理器杀死它。

如果有更多的事情要做,有人能给点提示吗?

EN

回答 1

Stack Overflow用户

发布于 2016-09-05 11:45:46

JeroMQ呼叫签名声明:

代码语言:javascript
复制
        /**
         * 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

和公认的一组标志

代码语言:javascript
复制
// ------------------------------------------------- // 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

结语:

如果使用.recv( 0 ),您的代码将阻塞,直到socket-instance确实从任何本地接收到任何.recv()-able,可能在不久或更远的将来存在,兼容的.connect()-ed远程终端(一个套接字等)。

如果使用{ <msg.data()_Value> | (EXC, NULL) },您的代码不会阻塞,但是应该正确地处理两个可能的返回状态,并与ZeroMQ errno details协商,以符合所指示的情况上下文。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39316544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档