title: Long running tasks with Python and Flask
body: <p>I have a rather large database import job that I start from a web interface (Flask). When visiting the URL that trigger the database import, I fork before doing database inserts.</p>
  
  <p>The problem is that when I attempt to stop the web service (Ctrl+C on the built in development server) the child process stops instead, and the web server keep running in the background. Somehow it appears as if the web server job is suddenly pushed to becoming a background daemon while the child process now is the primary process.</p>
  
  <p>What I want to achieve is a "start child process and then forget about it" approach where the web server just starts the child process and then never bother about it again, especially when it comes to exceptions and similar.</p>
  
  <p>Any ideas on how this is best solved?</p>
  
  <p>Currently the test code for the import function is:</p>
  
  <pre><code>def import_start():
      try:
              pid = os.fork()
      except OSError as e:
              print "Exception in import_start"
              sys.exit(1)
      if pid == 0:
            with open("/tmp/web_out.txt", "w") as f:
                  for x in range(100):
                        f.write("line %d\n" % (x))
                        f.flush()
                        sleep(10)
  </code></pre>
  
  <p>The code is started in a flask route handler with:</p>
  
  <pre><code>import_start()
  </code></pre>
  
  <p>Using Ctrl+C afterwards will kill the import_start() process instead of the web server. I want it to be the other way around as the two processes should be completely independent of each other after launch.</p>
  
  <p>UPDATE:</p>
  
  <p>I ended up doing:</p>
  
  <pre><code>def start_import():
              subprocess.Popen([sys.executable,__file__],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
  
  def do_import():
      with open("/tmp/web_out.txt", "w") as f:
              for x in range(100):
                      f.write("line %d\n" % (x))
                      f.flush()
                      sleep(10)
  
  if __name__ == "__main__":
      do_import()
  </code></pre>
  
  <p>This works (except for killing the spawned process when my web server is killed), but I am a bit worried about not cleaning up the process somehow. I might look into the MQ approach to solve this, although this requires more complexity than just spawning a new process.</p>