title: control a daemon thread with routes in flask
body: <p>How do you make two routes control a daemon thread in python</p>
  <p>flask backend file</p>
  <pre><code>from flask import Flask 
  from time import time,sleep
  from threading import Thread
  
  
  app = Flask(__name__)
  
  def intro():
      while True:
          sleep(3)
          print (f&quot; Current time : {time()}&quot;)
  
  
  @app.route('/startbot')
  def start_bot():
      global bot_thread 
      bot_thread = Thread(target=intro, daemon=True)
      bot_thread.start()
      return &quot;bot started &quot; 
  
  @app.route('/stopbot')
  def stop_bot():
      bot_thread.join()
      return 
  
  if __name__ == &quot;__main__&quot;:
      app.run()
  
  </code></pre>
  <p>When trying to kill the thread the curl request in the terminal does not return back to the console and the thread keeps on printing data to the terminal</p>
  <p>the idea I had was that I would declare the variable that holds the reference to the bot_thread and use the routes to control it<br />
  to test this I used <code>curl http://localhost:port/startbot</code> and <code>curl http://localhost:port/stopbot</code></p>
  <p>I can start the bot just fine but when I try to kill it, I get the following</p>
  <pre><code>NameError: name 'bot_thread' is not defined
  
  </code></pre>
  <p>Any help and does and don'ts will be very appreciated</p>
  <p>take into consideration that after killing the thread a user can create a new one and also be able to kill it</p>