2

所以我试图在 Python 中的 ZMQ 和 C/C++ 扩展中的 ZMQ 之间建立一些简单的通信。Python 设置上下文,绑定一个 inproc 套接字,并将上下文和套接字名称传递给扩展。扩展设置自己的套接字、连接和侦听消息。然后 Python 发送一个标题,然后将字典的字符串表示形式发送到扩展。使用 REQ/REP 套接字的非常简单的东西。但是,由于某种原因,我似乎找不到,对 socket.send 的调用被阻塞,并且扩展永远不会超过对 zmq_recv 的调用。我有一个测试环境,在那里我播放了几乎完全相同的场景,但套接字不会阻塞,并且我已经对代码进行了三重检查,它应该以相同的方式工作。

PYTHON:

import zmq
import cppextension
# No lectures about using threading please. I'm restricted to, in essence
# using this function because of the code base I'm working with.
from thread import start_new_thread

socket = self.zmq_context.socket(zmq.REQ)
socket_name = "inproc://agl"
socket.bind(socket_name)
t = start_native_thread(cppextension.actor,
                        (self.zmq_context, socket_name))

test_send = {"foo": 1, "bar": 2}
# BLOCKS ON THIS LINE VVVVV
socket.send("TEST", flags=zmq.SNDMORE)
socket.send(str(test_send))
socket.recv()
socket.send("STOP")

C/C++:

// Originally these used std::basic_string<Py_UNICODE> but I reverted
// back to normal std::string so I can use a JSON parsing library.
typedef string pystring;
typedef char pystring_t;

extern "C" PyObject *
actor(PyObject *self, PyObject *args) {
    PyObject *py_context, *py_connect_to;
    PyThreadState *_save;
    void *context;
    char *connect_to;
    void *socket;
    int rc;

    if(!PyArg_ParseTuple(args, "OO", &py_context, &py_connect_to)) {
        PyErr_SetString(PyExc_TypeError, "Expected two arguments (ZMQ context, name of socket to connect to)");
        return NULL;
    }
    py_context = PyObject_GetAttrString(py_context, "_handle");
    if(py_context == NULL) {
        PyErr_SetString(PyExc_TypeError, "Could not get '_handle' from context");
        return NULL;
    }
    if(!PyInt_Check(py_context)) {
        PyErr_SetString(PyExc_TypeError, "_handle was not an integer");
        return NULL;
    }
    context = (void*)PyInt_AsLong(py_context);
    connect_to = new char[PyString_Size(py_connect_to) + 1];
    strcpy(connect_to, PyString_AsString(py_connect_to));
    _save = PyEval_SaveThread();

    //
    // GIL-less operation BEGIN
    // ** WARNING: Do NOT call any functions that begin with 'Py', or touch any
    //    data structures that begin with 'Py' while in this section. It *WILL*
    //    blow up the Python interpreter.
    //
    socket = zmq_socket(context, ZMQ_REP);
    rc = zmq_connect(socket, connect_to);

    pystring TEST("TEST");
    pystring STOP("STOP");
    pystring SUCCESS("SUCCESS");
    pystring FAILURE("FAILURE");

    if(rc == 0) {
        int going = 1;
        // Should be able to hold a full megabyte of text, which should be enough
        // for any message being passed in.
        // Is there a way to query size of the incoming message...?
        char buffer[1000000];
        while(going) {
            // BLOCKS ON THIS LINE VVVVVV
            int size = zmq_recv(socket, buffer, 1000000, 0);
            if(size == -1) {
                // ERROR
                continue;
            }
            // Assume we don't get larger than 1MB of data. Should put a
            // check around this at some point, but not right now.
            buffer[size] = 0;

            pystring fullmsg(buffer);
            cout << "ZMQ RECIEVED: " << fullmsg << endl;
            if(fullmsg == TEST) {
                size = zmq_recv(socket, &buffer, 1000000, 0);
                if(size != -1) {
                    buffer[size] = 0;
                    pystring json_fullmsg(buffer);
                    cout << "ZMQ JSON: " << json_fullmsg << endl;
                    contacts.add(json_fullmsg);
                    zmq_send(socket, SUCCESS.c_str(), SUCCESS.size() + 1, 0);
                }
                else {
                    zmq_send(socket, FAILURE.c_str(), FAILURE.size() + 1, 0);
                }
            }
            else if(fullmsg == STOP) {
                going = 0;
                zmq_send(socket, SUCCESS.c_str(), SUCCESS.size() + 1, 0);
            }
        }
    }
    else {
        // ERROR
        int err = zmq_errno();
        switch(err) {
        case EINVAL:
            cout << "ZMQ CONNECT ERR: " << "Endpoint supplied is invalid" << endl;
            break;
        default:
            cout << "ZMQ CONNECT ERR: " << err << endl;
            break;
        }
    }
    zmq_close(socket);
    //
    // GIL-less operation END
    //

    PyEval_RestoreThread(_save);
    Py_INCREF(Py_None);
    return Py_None;
}

任何帮助弄清楚这里发生了什么都非常感谢。

编辑:另请注意,此代码将在 gevent 对标准库进行了猴子补丁的环境中运行。这是我使用thread.start_new_thread 的部分原因,因为它是在monkeypatching 发生之前保存的,我想要一个真正的线程而不是绿色线程。

4

2 回答 2

4

两件事情,

因为您在修改后的版本中使用了 req/rep,所以“发送、发送、接收、发送...”将不起作用。send/recv 都必须以“锁步”方式工作(send,recv,send,revc。)

ZMQ_NOBLOCK 会抛出 EAGAIN 异常,可能表示“socket 连接不完整,请稍后再来”。尝试在绑定和发送/接收后放置一个计时器/睡眠。这就是导致“资源暂时不可用”消息的原因。

希望这可以帮助

先生。开关

于 2013-10-18T11:19:39.687 回答
1

不确定这会有所帮助,但这里有:

  • 您确定会send()阻止吗,这不仅仅是永远得不到答复的问题吗?您可以调用send()withZMQ_NOBLOCK并查看是否引发异常。如果是,则确实send()无法将消息排入队列。
  • 您是否考虑过PAIR套接字而不是REQ/REP该指南建议将此用于inproc线程之间的通信。
于 2013-08-28T13:56:07.217 回答