title: PYZMQ REQ-REP Multiple Clients ZMQError with Polling
body: <p>With a <strong>REQ-REP</strong> pattern, I am trying to request multiple <code>clients</code> with a timeout using poll, so that if the <code>server</code> detects that it is not able to receive a message from the first <code>client</code>, it will timeout and move on to the next <code>client</code>.</p>
  
  <p>But it seems like after the initial timeout, it is unable to send the next message to the second <code>client</code>.</p>
  
  <p>I am getting this error <code>zmq.error.ZMQError: Operation cannot be accomplished in current state</code> on this line <code>socket.send_string("Sensor Data")</code> in <code>server</code>.</p>
  
  <p><strong>Full output</strong>:</p>
  
  <pre><code>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 &lt;module&gt;
      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]
  </code></pre>
  
  <p><strong>Server</strong>:</p>
  
  <pre><code>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")  # &lt;-- 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")
  </code></pre>
  
  <p><strong>Client</strong>:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>If the <code>server</code> was able to receive a message from the first <code>client</code>, the second <code>send</code> to the second <code>client</code> will work fine, but if the <code>server</code> was <strong>not</strong> able to receive a message from the first <code>client</code>, then the error is reproduced. </p>