title: I can't join background thread in flask with apache's mod_wsgi
body: <p>I'm building a web-server using Flask + Apache + mod_wsgi. I need to start a background thread in my web-server in order to do some stuff periodically. Everything fine, app started and thread works, but I can not join it. Below is some code snippets:</p>
  
  <h3>.wsgi file</h3>
  
  <pre><code>import sys, atexit, logging
  
  logging.basicConfig(filename='app.log', level=logging.INFO, filemode='w')
  logging.info('App started')
  
  sys.path.insert(0, '/my/src/dir/with/main.py/')
  
  from main import app, users_manager
  
  @atexit.register
  def on_exit_callback():
      logging.info('App will now be shutdowned')
      users_manager.set_shutting_down()
      users_manager.join_thread()
  
  application = app
  </code></pre>
  
  <h3>main.py</h3>
  
  <pre><code>from flask import Flask
  import logging
  from threading import Thread
  
  app = Flask(__name__)
  
  @app.route("/", methods=['GET'])
  def home():
     return "OK"
  
  class UsersManager():
      def __init__(self):
          self.users = {}
  
          self.shutdown_thread = False
  
          self.my_worker_thread = Thread(target=self.worker_function)
          self.my_worker_thread.start()
  
      def worker_function(self):
          while self.shutdown_thread is not True:
              # doing my stuff
  
      def set_shutting_down(self):
          self.shutdown_thread = True
  
      def join_thread(self):
          logging.info('joining thread')
          if self.my_worker_thread.is_alive():
              logging.info('thread is alive')
              self.my_worker_thread.join()
  
  users_manager = UsersManager()
  
  if __name__ == "__main__":
      app.run()
  </code></pre>
  
  <p>So, in <code>app.log</code> I see only 'App started'.</p>
  
  <p>Maybe, there are another ways to join my thread on apache and wsgi shutdown, not only using <code>atexit</code> module.</p>