I am trying to get a python program to communicate with another python program through zeromq using a request-response template. The client program must send a request to the server program that is responding.
I have two servers, so when one server fails, the other takes over. However, the connection works fine when the first server is working, however, when the first server fails, and when I make a request to the second server, I see an error:
zmp.error.ZMQError: operation cannot be performed in the current state
Server 1 Code:
# Run the server while True: # Define the socket using the "Context" sock = context.socket(zmq.REP) sock.bind("tcp://127.0.0.1:5677") data = sock.recv().decode("utf-8") res = "Recvd" sock.send(res.encode('utf-8'))
Server 2 Code:
# Run the server while True: # Define the socket using the "Context" sock = context.socket(zmq.REP) sock.bind("tcp://127.0.0.1:5877") data = sock.recv().decode("utf-8") res = "Recvd" sock.send(res.encode('utf-8'))
Client code:
# ZeroMQ Context For distributed Message amogst processes context = zmq.Context() sock_1 = context.socket(zmq.REQ) sock_2 = context.socket(zmq.REQ) sock_1.connect("tcp://127.0.0.1:5677") sock_2.connect("tcp://127.0.0.1:5877") try: sock_1.send(data.encode('utf-8'), zmq.NOBLOCK) socks_1.setsockopt(zmq.RCVTIMEO, 1000) socks_1.setsockopt(zmq.LINGER, 0) data = socks_1.recv().decode('utf-8')
What is the problem with this approach? How can i solve this?
source share