title: Why does a background task block the response in SimpleHTTPServer?
body: <p>I'm writing a simple browser-based front end that should be able to launch a background task and then get progress from it.  I want the browser to receive a response saying whether the task launched successfully, and then poll to determine when it is done.  However, the presence of a background task seems to be stopping the XMLHttpRequest response from being sent immediately, so I can't report the success of launching the process.  Consider the following (simplified) code:</p>
  
  <pre><code>import SocketServer
  import SimpleHTTPServer
  import multiprocessing
  import time
  
  class MyProc(multiprocessing.Process):
      def run(self):
          print 'Starting long process..'
          for i in range(100): time.sleep(1)
          print 'Done long process'
  
  class Page(SimpleHTTPServer.SimpleHTTPRequestHandler):
      def do_GET(self):
          if self.path == '/':
              print &gt;&gt;self.wfile, "&lt;html&gt;&lt;body&gt;&lt;a href='/run'&gt;Run&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;"
          if self.path == '/run':
              self.proc = MyProc()
              print 'Starting..'
              self.proc.start()
              print 'After start.'
              print &gt;&gt;self.wfile, "Process started."
  
  httpd = SocketServer.TCPServer(('', 8000), Page)
  httpd.serve_forever()
  </code></pre>
  
  <p>When I run this, and browse to <a href="http://localhost:8000" rel="nofollow">http://localhost:8000</a>, I get a button named "Run".  When I click on it, the terminal displays:</p>
  
  <pre><code>Starting..
  After start.
  </code></pre>
  
  <p>However the browser view does not change.. in fact the cursor is spinning.  Only when I press Ctrl-C in the terminal to interrupt the program, then the browser is update with the message <code>Process started.</code></p>
  
  <p>The message <code>After start</code> is clearly being printed.  Therefore I can assume that <code>do_GET</code> is returning after starting the process.  Yet, the browser doesn't get a response until after I interrupt the long-running process.  I have to conclude there is something blocking between <code>do_GET</code> and the response being sent, which is inside <code>SimpleHTTPServer</code>.</p>
  
  <p>I've also tried this with threads and subprocess.Popen but ran into similar problems. Any ideas?</p>