gpt4 book ai didi

python - PYZMQ REQ-REP 多客户端 ZMQError 与轮询

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:10 26 4
gpt4 key购买 nike

使用 REQ-REP 模式,我尝试使用轮询请求多个带有超时的 clients ,这样,如果 server 检测到它无法从第一个 client 接收消息,它将超时并转到下一个 client

但似乎在初始超时后,无法将下一条消息发送到第二个 client

我在 zmq.error.ZMQError: Operation cannot be accomplished in current state 中的 socket.send_string("Sensor Data") 行上收到此错误 server

完整输出:

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]

服务器:

import zmq
import json

ports = ["127.0.0.1:9999", "127.0.0.1:9998"]

context = zmq.Context()
print("Connecting to machine...")
socket = context.socket(zmq.REQ)
socket.setsockopt(zmq.LINGER, 0)
for port in ports:
socket.connect("tcp://%s" % port)
print("Successfully connected to machine %s" % port)

for request in range(len(ports)):
print("Sending request ", request, "...")
socket.send_string("Sensor Data") # <-- error occurs here

# use poll for timeouts:
poller = zmq.Poller()
poller.register(socket, zmq.POLLIN)

socks = dict(poller.poll(5 * 1000))

if socket in socks:
try:
msg_json = socket.recv()
sens = json.loads(msg_json)
response = "Sensor: %s :: Data: %s :: Client: %s" % (sens['sensor'], sens['data'], sens['client'])
print("Received reply ", request, "[", response, "]")
except IOError:
print("Could not connect to machine")
else:
print("Machine did not respond")

客户:

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 将正常工作,但如果 server 无法能够从第一个 client 接收消息,然后重现错误。

最佳答案

最重要的是,zmq.error.ZMQError: Operation cannot be accomplished in current state REQ-REP 模式中表示 send -> recv -> send -> recv 的顺序不正常。就我而言,由于 for-loop 中接收的民意调查没有最终的recvREQ 服务器 端超时。当该方法循环回来时,它转到 send再次导致 send -> recv -> send -> timeout -> send 。双send这是非法的场景。

我做了什么来纠正它:我从 REQ-REP 模式切换到 DEALER-REP 模式。这为我提供了一个可以与多个 REP 客户端通信的异步服务器。

client保持不变,这是新的 server对于那些有兴趣的人:

服务器:

import zmq
import json

ports = ["127.0.0.1:9999", "127.0.0.1:9998"]

context = zmq.Context()
print("Connecting to machine...")
socket = context.socket(zmq.DEALER)
socket.setsockopt(zmq.LINGER, 0)
for port in ports:
socket.connect("tcp://%s" % port)
print("Successfully connected to machine %s" % port)

for request in range(len(ports)):
print("Sending request ", request, "...")
socket.send_string("", zmq.SNDMORE) # delimiter
socket.send_string("Sensor Data") # actual message

# use poll for timeouts:
poller = zmq.Poller()
poller.register(socket, zmq.POLLIN)

socks = dict(poller.poll(5 * 1000))

if socket in socks:
try:
socket.recv() # discard delimiter
msg_json = socket.recv() # actual message
sens = json.loads(msg_json)
response = "Sensor: %s :: Data: %s :: Client: %s" % (sens['sensor'], sens['data'], sens['client'])
print("Received reply ", request, "[", response, "]")
except IOError:
print("Could not connect to machine")
else:
print("Machine did not respond")

关于python - PYZMQ REQ-REP 多客户端 ZMQError 与轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48414559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com