title: Processing Simultaneous/Asynchronous Requests with Python BaseHTTPServer
body: <p>I've set up a threaded (with Python threads) HTTP server by creating a class that inherits from HTTPServer and ThreadingMixIn:</p>
  
  <pre><code>class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
      pass
  </code></pre>
  
  <p>I have a handler class which inherits from BaseHTTPRequestHandler, and I start the server with something like this:</p>
  
  <pre><code>class MyHandler(BaseHTTPRequestHandler):
      ...
  
  server = ThreadedHTTPServer(('localhost', 8080), MyHandler)
  # Prevent issues with socket reuse
  server.allow_reuse_address = True
  # Start the server
  server.serve_forever()
  </code></pre>
  
  <p>This is all pretty straightforward. The problem that I'm encountering is that, ThreadingMixIn, ForkingMixIn, or otherwise, the request winds up blocking on the request handler to return. This can easily be seen by implementing this example code:</p>
  
  <pre><code>class MyHandler(BaseHTTPRequestHandler):
      def respond(self, status_code):
          self.send_response(status_code)
          self.end_headers()
  
      def do_GET(self):
           print "Entered GET request handler"
           time.sleep(10)
           print "Sending response!"
           respond(200)
  </code></pre>
  
  <p>If the server were processing these simultaneously, then we would be able to send two requests and see the server enter both GET request handlers before sending either response. Instead, the server will enter the GET request handler for the first request, wait for it to return, then enter it for the second (so the second request takes ~20 seconds to return instead of 10).</p>
  
  <p>Is there a straightforward way for me to implement a system where the server doesn't wait on the handler to return? Specifically, I'm trying to write a system which waits to receive several requests before returning any of them (a form of long polling) and running into issues where the first request waiting blocks any future requests from connecting to the server.</p>