What I am trying to do:
I am trying to kill a thread that has an infinite loop from a parent process launched. I want the child process to be killed as soon as I change the variable.
What I have done so far:
I have shared a variable with the two threads and once the loop gets the stop signal it stops.
Whats not working:
It is not killing the thread as soon as I send the stop signal
Here is some sample code:
from flask import Flask, flash, redirect, render_template, request, session, abort
import light_control
import logging
import thread
app = Flask(__name__)
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
global stop
stop = False
def loop_rainbow_cycle_successive(name):
global stop
while 1:
if stop == True:
break
light_control.rainbow_cycle_successive()
def loop_rainbow_cycle(name):
global stop
while 1:
if stop == True:
break
light_control.rainbow_cycle()
def loop_rainbow_colors(name):
global stop
while 1:
if stop == True:
break
light_control.rainbow_colors()
RGB = ()
@app.route("/")
def index():
return render_template(
'index.html')
@app.route("/getColor/", methods=['POST'])
def getColor():
RGB = (request.form['r'],request.form['g'],request.form['b'])
light_control.setColor(RGB)
return render_template('index.html')
@app.route("/useFunction/", methods=['POST'])
def useFunction():
global stop
stop = False
func = request.form['function']
if func == "rainbow_cycle_successive":
thread.start_new_thread( loop_rainbow_cycle_successive, ("loop_rainbow_cycle_successive", ))
elif func == "rainbow_cycle":
thread.start_new_thread( loop_rainbow_cycle, ("loop_rainbow_cycle", ))
elif func == "rainbow_colors":
thread.start_new_thread( loop_rainbow_colors, ("loop_rainbow_cycle", ))
elif func == "STOP":
stop = True
return render_template('index.html')
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)