Python + ZMQ: operation cannot be performed in current state

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') #receive data from the main node except: try: #when server one fails sock_2.send(data.encode('utf-8'), zmq.NOBLOCK) socks_2.setsockopt(zmq.RCVTIMEO, 1000) socks_2.setsockopt(zmq.LINGER, 0) data = socks_2.recv().decode('utf-8') except Exception as e: print(str(e)) 

What is the problem with this approach? How can i solve this?

+5
source share
1 answer

Q: How can I solve this?
A: Avoid the known risk of blocking REQ/REP !

While ZeroMQ is a powerful platform, an understanding of its internal composition is necessary for the reliable and reliable design and prototyping of distributed systems.

On closer inspection, using the usual REQ/REP formal communication template may leave (and leave) the counterparties in a mutual lock, where one expects the other to take a step that will never be completed and there is no way out of such an impasse.

For more detailed illustrated information and diagrams of the FSA diagram, see this post.

Further , the fault tolerance system must withstand any collisions of its own components. Thus, it is more necessary to produce a good signaling of the state of the distributed system and to avoid as much dependencies as possible on the FSA design element / step / lock, otherwise the fault-tolerant behavior remains just an illusion.

Always handle resources with care, do not consider ZeroMQ smart alarm / messaging components as any “consumable consumables", so this can be allowed in the examples of a scientist, and not in a production environment system. You still have to pay the costs (time, resource allocation / allocation / garbage collection). As noted in the comments, never allow the creation / allocation of resources without proper control. while True: .socket(); .bind(); .send(); is a gross violation in principle and degrades the rest of the design.

+3
source

Source: https://habr.com/ru/post/1260913/


All Articles