对于一个REQ-REP模式,我试图使用poll请求多个具有超时的clients
,这样如果server
检测到它无法从第一个client
接收消息,它将超时并继续到下一个client
。在
但在初始超时之后,它似乎无法将下一条消息发送到第二条client
。在
我在server
中的zmq.error.ZMQError: Operation cannot be accomplished in current state
这行socket.send_string("Sensor Data")
上得到这个错误。在
全输出:
Connecting to machine...
Successfully connected to machine 127.0.0.1:9999
Successfully connected to machine 127.0.0.1:9998
Sending request 0 ...
Machine did not respond
Sending request 1 ...
Traceback (most recent call last):
File "C:\Users\tobiw\Documents\Python\Raspberry Pi\zmq\REQrep\testSIMPLEpoll.py", line 16, in <module>
socket.send_string("Sensor Data")
File "C:\Users\tobiw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyzmq-17.0.0b3-py3.6-win32.egg\zmq\sugar\socket.py", line 541, in send_string
return self.send(u.encode(encoding), flags=flags, copy=copy, **kwargs)
File "C:\Users\tobiw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyzmq-17.0.0b3-py3.6-win32.egg\zmq\sugar\socket.py", line 384, in send
return super(Socket, self).send(data, flags=flags, copy=copy, track=track)
File "zmq/backend/cython/socket.pyx", line 727, in zmq.backend.cython.socket.Socket.send
File "zmq/backend/cython/socket.pyx", line 771, in zmq.backend.cython.socket.Socket.send
File "zmq/backend/cython/socket.pyx", line 249, in zmq.backend.cython.socket._send_copy
File "zmq/backend/cython/socket.pyx", line 244, in zmq.backend.cython.socket._send_copy
File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
raise ZMQError(errno)
zmq.error.ZMQError: Operation cannot be accomplished in current state
[Finished in 5.3s with exit code 1]
服务器:
^{pr2}$客户:
import zmq
import time
import json
port = "9998" # multiple similar clients but just with different ports
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:%s" % port)
while True:
# Wait for next request from server
message = str(socket.recv(), "utf-8")
print("Received request: ", message)
time.sleep(1)
msgDict = {
'sensor': "6",
'data': "123456789",
'client': "9876",
}
msg_json = json.dumps(msgDict)
socket.send_string(msg_json)
如果server
能够接收来自第一个client
的消息,那么第二个send
到第二个client
将正常工作,但是如果{client
接收消息,那么错误就会重现。在
最重要的是,REQ-REP模式中的}的顺序不正确。在我的例子中,由于在}。非法的双
zmq.error.ZMQError: Operation cannot be accomplished in current state
表示{for-loop
中对receive进行轮询,REQ server端没有最终的recv
,因为它超时了。当方法循环返回时,它再次转到send
,结果是{send
方案。在我做了什么来纠正它: 我从REQ-REP模式切换到经销商代表模式。这为我提供了一个异步服务器,可以与多个REP客户机进行通信。在
在
client
保持不变的情况下,对于感兴趣的人来说,这是一个新的server
:服务器:
相关问题 更多 >
编程相关推荐