title: Python: How to shutdown a threaded HTTP server with persistent connections (how to kill readline() from another thread)?
body: <p>I'm using python2.6 with <code>HTTPServer</code> and the <code>ThreadingMixIn</code>, which will handle each request in a separate thread.  I'm also using HTTP1.1 persistent connections ('Connection: keep-alive'), so neither the server or client will close a connection after a request.</p>
  
  <p>Here's roughly what the request handler looks like</p>
  
  <pre><code>request, client_address = sock.accept()
  rfile = request.makefile('rb', rbufsize)
  wfile = request.makefile('wb', wbufsize)
  
  global server_stopping
  while not server_stopping:
      request_line = rfile.readline() # 'GET / HTTP/1.1'
      # etc - parse the full request, write to wfile with server response, etc
  wfile.close()
  rfile.close()
  request.close()
  </code></pre>
  
  <p>The problem is that if I stop the server, there will still be a few threads waiting on <code>rfile.readline()</code>.</p>
  
  <p>I would put a <code>select([rfile, closefile], [], [])</code> above the <code>readline()</code> and write to closefile when I want to shutdown the server, but I don't think it would work on windows because <code>select</code> only works with sockets.</p>
  
  <p>My other idea is to keep track of all the running requests and <code>rfile.close()</code> but I get Broken pipe errors.</p>
  
  <p>Ideas?</p>