title: Why is socket behaviour dependant on a print statement?
body: <p>I do not understand how something like a print statement can affect socket behaviour. The following code was run on windows, from idle and cmd.</p>
  
  <p>server.py: </p>
  
  <pre class="lang-py prettyprint-override"><code>from types import SimpleNamespace
  import socket
  import selectors
  
  HOST = '127.0.0.1'
  PORT = 65433
  
  sel = selectors.DefaultSelector()
  
  server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server_sock.bind((HOST, PORT))
  server_sock.listen()
  print('Server listening on', (HOST, PORT))
  
  server_sock.setblocking(False)
  sel.register(server_sock, selectors.EVENT_READ, data=None)
  
  def accept_new_connection(server_sock):
      connected_sock, addr = server_sock.accept()
      connected_sock.setblocking(False)
  
      events = selectors.EVENT_READ | selectors.EVENT_WRITE
      data = SimpleNamespace(addr = addr, outb = b'', inb = b'')
      sel.register(connected_sock, events, data=data)
  
      print('new connection', addr, 'accepted')
  
  def service_connection(key, event):
      connected_sock = key.fileobj
      data = key.data
  
      if event &amp; selectors.EVENT_READ:
          recv_data = connected_sock.recv(1024)
          if data:
              data.outb += recv_data
          else:
              print('closing the connection', data.addr)
              sel.unregister(connected_sock)
              connected_sock.close()
  
      elif event &amp; selectors.EVENT_WRITE:
          if data.outb:
              print('echoing', data.outb, 'to', data.addr)
              sent = connected_sock.send(data.outb)
              data.outb = data.outb[sent:]
              print('ANYTHING') # removing this changes socket behaviour
          else:
              # this executes if print('ANYTHING') is not present
              print('nothing to send') 
  
  while True:
      events = sel.select(timeout=None)
      for key, event in events:
          if key.data == None:
              accept_new_connection(key.fileobj)
          else:
              service_connection(key, event)
  </code></pre>
  
  <p>client.py: </p>
  
  <pre class="lang-py prettyprint-override"><code>import socket
  
  HOST = '127.0.0.1'
  PORT = 65433
  
  ls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  ls.connect((HOST, PORT))
  ls.send(b'Hi world')
  ls.recv(1024)
  ls.close()
  </code></pre>
  
  <p>Running those two programs (first server, then client) 
  producess different results, depending on whether 
  <code>print('ANYTHING')</code> is present. </p>
  
  <p>When run with the <code>print('ANYTHING')</code> included, 
  I get </p>
  
  <pre><code>Server listening on ('127.0.0.1', 65433)
  new connection ('127.0.0.1', 58675) accepted
  echoing b'Hi world' to ('127.0.0.1', 58675)
  ANYTHING
  </code></pre>
  
  <p>and without it</p>
  
  <pre><code>Server listening on ('127.0.0.1', 65433)
  new connection ('127.0.0.1', 58678) accepted
  echoing b'Hi world' to ('127.0.0.1', 58678)
  nothing to send
  </code></pre>