ZeroMQ 操作抛出 EXC: [Operation cannot be completed in current state]

ZeroMQ operation throws EXC: [ Operation cannot be accomplished in current state ]

我正在尝试制作一个能够发送数据然后接收数据的 class。

目前,它仅对第一个 send/receive 有效,如果再次尝试 .send(),它将在下方抛出错误。

>Traceback (most recent call last):
  File "main.py", line 31, in <module>
    zq.send(arr)
  File "D:\ITIM\video2\MQCompare\cZMQ.py", line 17, in send
    self.socketC.send(data)
  File "zmq/backend/cython/socket.pyx", line 636, in zmq.backend.cython.socket.S
ocket.send (zmq\backend\cython\socket.c:7305)
  File "zmq/backend/cython/socket.pyx", line 683, in zmq.backend.cython.socket.S
ocket.send (zmq\backend\cython\socket.c:7048)
  File "zmq/backend/cython/socket.pyx", line 206, in zmq.backend.cython.socket._
send_copy (zmq\backend\cython\socket.c:3032)
  File "zmq/backend/cython/socket.pyx", line 201, in zmq.backend.cython.socket._
send_copy (zmq\backend\cython\socket.c:2920)
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc.
_check_rc (zmq\backend\cython\socket.c:10014)
    raise ZMQError(errno)
zmq.error.ZMQError: Operation cannot be accomplished in current state`

我使用的代码如下所示:

import zmq

class ZeroMQ:

def __init__(self):
    self.context = zmq.Context()
    self.socketS = self.context.socket(zmq.REP)
    self.socketS.bind("tcp://*:5555")
    self.socketC = self.context.socket(zmq.REQ)
    self.socketC.connect("tcp://localhost:5555")

def __exit__(self, exc_type, exc_value, traceback):
    self.socketC.close()
    self.socketS.close()

def send(self, data):
    self.socketC.send(data)

def recv(self):    
    self.socketS.recv()

我连接正确吗?

为什么函数 send 会抛出错误?

如有任何帮助,我将不胜感激。谢谢。

仅仅因为 socketC 作为 REQ 原型的实例,永远无法发送另一个原型消息(正如上面 .send() class-method 的实现中所编码的那样),而无需事先调用 socketC.recv() instance-method.

REQREP 原型都被证明必须遵守 two-step .send()-.recv()-.send()-.recv()-... 的舞蹈。 .recv()-.send()-.recv()-... 使用他们的母语 instance-methods.

这就是 ZeroMQ REQ / REP 套接字的设计和记录方式。

如果您的消息是单向的,请考虑使用 ZMQ_PUSH / ZMQ_PULL 对。