Im trying to start the main function on a new process (because it's a loop) with initial values. I want to be able to restart the main function with different arguments which i get as a Webinput. So im trying to kill the process and then restart it with the right Webinputs as the arguments. I also want to be able to change the values that main is using at any time using my webinterface.
Code: Select all
from flask import Flask, request, render_template import RPi.GPIO as GPIO import multiprocessing import time app = Flask(__name__) pin = 17 btn = 22 def setup(): GPIO.setmode(GPIO.BCM) GPIO.setup(pin, GPIO.OUT) GPIO.setup(btn, GPIO.IN) GPIO.output(pin, GPIO.HIGH) def main(hours, minutes, seconds): while True: while time.strftime('%H:%M:%S') != str(hours)+":"+str(minutes)+":"+str(seconds): if GPIO.input(btn) == GPIO.HIGH: GPIO.output(pin, GPIO.LOW) time.sleep(0.01) else: GPIO.output(pin, GPIO.HIGH) time.sleep(0.01) GPIO.output(pin, GPIO.LOW) time.sleep(2) GPIO.output(pin, GPIO.HIGH) @app.route('/') def home(): return render_template('home.html') @app.route('/', methods=['POST']) def HomePost(): inputdir = request.form print(inputdir) hours = inputdir["hours"] minutes = inputdir["minutes"] seconds = inputdir["seconds"] p1.terminate() p1=multiprocessing.Process(target = main, args = (hours, minutes, seconds)) p1.start() return render_template('home.html') if __name__ == "__main__": setup() p1 = multiprocessing.Process(target = main, args = (16, 00, 00)) p1.start() app.run(host='0.0.0.0')
The problem is that the process variable p1 is locale so i can't terminate it with p1.terminate() in the HomePost function which is called by flask whenever i submit my inputs. Whenever the HomePost function gets called i get the error: UnboundLocalError: loval variable 'pi' referenced before assignment
How can i kill the process with the HomePost function?