我要做的是:
我正在尝试从已启动的父进程中杀死一个具有无限循环的线程。我希望在更改变量后立即杀死子进程。
到目前为止我做了什么:
我与两个线程共享了一个变量,一旦循环得到停止信号就停止了。
什么不行?
一旦发送停止信号
,就不会杀死线程以下是一些示例代码:
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)
答案 0 :(得分:0)
您可以将其设为流程并使用p.terminate()
代码:
from flask import Flask, flash, redirect, render_template, request, session, abort
import sys, os
sys.path.append(os.getcwd() + '/audio-reactive-led-strip/python/')#append entire directory
import logging
import multiprocessing
import light_control, visualization
app = Flask(__name__)
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
global stop
stop = False
p = multiprocessing.Process()
def terminate_process():
global p
if p.is_alive():
p.terminate()
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()
def loop_visualization(name):
# Initialize LEDs
visualization.led.update()
# Start listening to live audio stream
visualization.microphone.start_stream(visualization.microphone_update)
RGB = ()
@app.route("/")
def index():
return render_template(
'index.html')
@app.route("/getColor/", methods=['POST'])
def getColor():
terminate_process()
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
global p
stop = False
func = request.form['function']
if func == "rainbow_cycle_successive":
terminate_process()
p = multiprocessing.Process(target=loop_rainbow_cycle_successive, args=("loop_rainbow_cycle_successive", ))
p.start()
elif func == "rainbow_cycle":
terminate_process()
p = multiprocessing.Process(target=loop_rainbow_cycle, args=("loop_rainbow_cycle", ))
p.start()
elif func == "rainbow_colors":
terminate_process()
p = multiprocessing.Process(target=loop_rainbow_colors, args=("loop_rainbow_cycle", ))
p.start()
elif func == "visualization":
terminate_process()
p = multiprocessing.Process(target=loop_visualization, args=("loop_visualization", ))
p.start()
elif func == "STOP":
stop = True
terminate_process()
return render_template('index.html')
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)