Lets say I have a very simple Client/Server model, using REQ/REP
from ZeroMQ. See python code below.
In the code below the client will wait forever, but I want the client to give up (lets say after 20 seconds) and move on with its life if it doesn't get a response. The server could be down, the router unplugged, the WiFi is not working. I really don't or should care why.
Then at a later time, I'll have the client try again and it could be a completely different request.
But I fear I'll cross an old request, get things out of order, cause more problems.
Does anyone know how to do this gracefully? I've been racking my brain on a simple solution.
SIMPLE CLIENT CODE
#!/usr/bin/env python3
import zmq
from time import sleep
# CREATE SOCKET - Client (USING zmq.REQ)
my_client_context = zmq.Context()
my_client_socket = my_client_context.socket(zmq.REQ)
my_client_socket.connect('tcp://127.0.0.1:5557')
# [REQ]uest AND [REP]ly
to_server = b"Hi"
my_client_socket.send(to_server)
from_server = my_client_socket.recv()
print(from_server)
sleep(2)
# REQuest AND REPort
to_server = b"blah"
my_client_socket.send(to_server)
from_server = my_client_socket.recv()
print(from_server)
SIMPLE SERVER CODE
#!/usr/bin/env python3
import zmq
# CREATE SOCKET - Server (USING zmq.REP)
my_server_context = zmq.Context()
my_server_socket = my_server_context.socket(zmq.REP)
my_server_socket.bind('tcp://127.0.0.1:5557')
# LISTEN ON SOCKET
while True:
msg = my_server_socket.recv()
if msg == b'Hi':
to_client = b"Well hello to you"
my_server_socket.send(to_client)
else:
to_client = b"Not sure what you want"
my_server_socket.send(to_client)