title: Python non-blocking socket - Receiving message after delay
body: <p>I am trying to make my client receive a message from my server using a non-blocking Socket, however, the client is only allowed to check for new messages once every second. The server first sends the size of the message as an integer, and then the message itself.</p>
  <p>This is the code on my client:</p>
  <pre><code>server = ('192.168.1.220', 4000)
  buf_size = 16384
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.connect(server)
  sock.setblocking(0) 
  
  while True:
      message = b''
      size = None
  
      try:
          size = int(sock.recv(buf_size).decode('utf-8'))
      except:
          pass
      
      if size:
          while len(message) &lt; size:
              try:
                  data = sock.recv(buf_size)
                  message += data
              except:
                  pass
  
      if len(message) &gt; 0:
          print(message)
      
      #time.sleep(1)
    
  </code></pre>
  <p>This works as it is, however, if I uncomment the 1 second delay at the end of the outer while loop (<code>time.sleep(1)</code>), the client does not receive any messages the server sends anymore (there are no errors either).</p>
  <p>Why does the delay break the code here? I assume the messages sent to the client will remain in the stream until they are read by the <code>recv()</code> method. Am I misunderstanding the way nonblocking sockets work? How would I be able to achieve this instead?</p>