title: Terminating GAE Python Background Threads
body: <p>What is the correct way of shutting down Google App Engine Background Threads? I have tried setting a loop variable to false and trying to join() the thread but regardless of what I try I get many calls to _/ah/stop ending finally with a "Process terminated because the backend took too long to shutdown."</p>
  
  <p>It works but does not feel very stable, is there a better way of shutting them down?</p>
  
  <p>some code:</p>
  
  <pre><code>_thread = None`
  _should_run = True
  
  def work():
      service = BqClient.getService()
      tabledata = service.tabledata()
  
      while (_should_run and not runtime.is_shutting_down()):
          try: 
              queue = taskqueue.Queue('stream-queue')
              tasks = queue.lease_tasks(300, 1000)
              ...do stuff and then sleep...
  
  def startStreamThread():
      _thread = BackgroundThread(target = work)
      _thread.start()
  
  def stopStreamThread():
      _should_run = False
      if (_thread and _thread.isAlive()):
          _thread.join(20)
  </code></pre>