title: Start background process/daemon from CGI script
body: <p>I'm trying to launch a background process from a CGI scripts.  Basically, when a form is submitted the CGI script will indicate to the user that his or her request is being processed, while the background script does the actual processing (because the processing tends to take a long time.)  The problem I'm facing is that Apache won't send the output of the parent CGI script to the browser until the child script terminates.</p>
  
  <p>I've been told by a colleague that what I want to do is impossible because there is no way to prevent Apache from waiting for the entire process tree of a CGI script to die.  However, I've also seen numerous references around the web to a "double fork" trick which is supposed to do the job.  The trick is described succinctly in <a href="https://stackoverflow.com/questions/1952900/running-c-cgi-script-as-background-process/1954215#1954215">this Stack Overflow answer</a>, but I've seen similar code elsewhere.</p>
  
  <p>Here's a short script I wrote to test the double-fork trick in Python:</p>
  
  <pre><code>import os
  import sys
  
  if os.fork():
      print 'Content-type: text/html\n\n Done'
      sys.exit(0)
  
  if os.fork():
      os.setsid()
      sys.exit(0)
  
  # Second child
  os.chdir("/")
  sys.stdout.close()
  sys.stderr.close()
  sys.stdin.close()
  
  f = open('/tmp/lol.txt', 'w')
  
  while 1:
       f.write('test\n')
  </code></pre>
  
  <p>If I run this from the shell, it does exactly what I'd expect: the original script and first descendant die, and the second descendant keeps running until it's killed manually.  But if I access it through CGI, the page won't load until I kill the second descendant or Apache kills it because of the CGI timeout.  I've also tried replacing the second <code>sys.exit(0)</code> with <code>os._exit(0)</code>, but there is no difference.</p>
  
  <p>What am I doing wrong?</p>