title: ZeroMQ - how to make a CLIENT to give up and try at a later time if the SERVER doesn't respond?
body: <p>Lets say I have a very simple Client/Server model, using <strong><code>REQ/REP</code></strong> from ZeroMQ. See python code below.</p>
  
  <p>In the code below the client will wait forever, but <strong>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.</strong> The server could be down, the router unplugged, the WiFi is not working.  I really don't or should care why.</p>
  
  <p>Then at a later time, I'll have the client try again and it could be a completely different request.</p>
  
  <p>But I fear I'll cross an old request, get things out of order, cause more problems.</p>
  
  <p>Does anyone know how to do this gracefully?  I've been racking my brain on a simple solution. </p>
  
  <p><strong>SIMPLE CLIENT CODE</strong></p>
  
  <pre><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)
  </code></pre>
  
  <p><strong>SIMPLE SERVER CODE</strong></p>
  
  <pre><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)
  </code></pre>