title: Properly terminate flask web app running in a thread
body: <p>How to properly terminate a flask web application that was launched in a separate thread? I found an incomplete <a href="https://stackoverflow.com/questions/31264826/start-a-flask-application-in-separate-thread?utm_medium=organic&amp;utm_source=google_rich_qa&amp;utm_campaign=google_rich_qa">answer</a> that is not clear on how to do it. The script below starts a thread which in turn starts a flask application. When I press <kbd>CTRL</kbd>+<kbd>C</kbd>, something is not being terminated and the script never stops. It would be nice to add the code after <code>except KeyboardInterrupt:</code> that terminates the <code>app</code> and the <code>thread_webAPP()</code> properly. I know how to terminate a thread, but first I need to terminate the app:</p>
  
  <pre><code>def thread_webAPP():
      app = Flask(__name__)
  
      @app.route("/")
      def nothing():
          return "Hello World!"
  
      app.run(debug=True, use_reloader=False)
      # hope that after app.run() is terminated, it returns here, so this thread could exit
  
  
  t_webApp = threading.Thread(name='Web App', target=thread_webAPP)
  t_webApp.start()
  
  try:
      while True:
          time.sleep(1)
  
  except KeyboardInterrupt:
      print("exiting")
      # Here I need to kill the app.run() along with the thread_webAPP
  </code></pre>