title: How to start a new thread in Flask? Thread can only be started once
body: <p>I am using the Flask webframe to control a sensor (start and stop scanning) which is connected to a Raspberry Pi 3. Once I stop scanning (as the Event is set), I cannot start a new thread because the thread can only be started once. So any tip to start a new thread with a different variable name? or any alternatives?</p>
  
  <pre><code>import thread
  from flask import Flask, request, url_for, redirect, render_template
  
  def scan(number, duration):
      #Scanner scanning with 2 parameters
  
  class LoopThread(threading.Thread):
      def __init__(self, name, event):
          super(LoopThread,self).__init__()
          self.name = name #can this use as a global variable?
          self.event = event
          self.number = 20
          self.duration = 1
  
      def run(self):
          print('Starting Thread-'+ str(self.name))
          while not self.event.wait(timeout=1.0):
              self.loop_process()
  
      def loop_process(self):
          scan(self.number, self.duration)
  
  app = Flask(__name__)
  
  stopevent = threading.Event()
  thread = LoopThread(1, stopevent) #what can I change here?
  
  @app.route("/")
  def index():
      print("home page")
      return render_template('home.html')
  
  @app.route("/start")
  def start():
      thread.start() #how do I change the function variable name everytime?
      return redirect(url_for('index'))
  
  @app.route("/stop")
  def stop():
      stopevent.set() #stop the senseor scanning
      thread.join()
      return redirect(url_for('index'))
  
  if __name__ == '__main__':
      app.run("0.0.0.0", debug=True)
  </code></pre>