title: Python sockets buffering
body: <p>Let's say I want to read a line from a socket, using the standard <code>socket</code> module:</p>
  
  <pre><code>def read_line(s):
      ret = ''
  
      while True:
          c = s.recv(1)
  
          if c == '\n' or c == '':
              break
          else:
              ret += c
  
      return ret
  </code></pre>
  
  <p>What exactly happens in <code>s.recv(1)</code>? Will it issue a system call each time? I guess I should add some buffering, anyway:</p>
  
  <blockquote>
    <p>For best match with hardware and network realities, the value of <em>bufsize</em> should be a relatively small power of 2, for example, 4096.</p>
  </blockquote>
  
  <p><a href="http://docs.python.org/library/socket.html#socket.socket.recv" rel="noreferrer">http://docs.python.org/library/socket.html#socket.socket.recv</a></p>
  
  <p>But it doesn't seem easy to write efficient and thread-safe buffering. What if I use <code>file.readline()</code>?</p>
  
  <pre><code># does this work well, is it efficiently buffered?
  s.makefile().readline()
  </code></pre>