0

I'm running a flask webserver with a raspberry pi 3B+. The html website has some text, and two buttons. One button to run a external python script (not the flask python script) and one button to stop the python script.

So far, pressing the "start" button makes my external python script run just how I want it to. That works perfectly and I have no complaints about that

I want my "stop" button to stop the external python script that I started running with the "start" button. The script has a never ending while loop in it.

I've tried everything I can think of:

  • creating a 'running' boolean variable, so that the while loop only runs when that variable equals true, and then using the stop button to change that variable.
  • Having a "exit()" function inside of my external script that I call when my "stop" button is pressed.

None of that works. It just causes my webserver to crash and my external script keeps running (lol)

I get "TypeError: The view function did not return a valid response."

My flask code:

from flask import Flask, render_template
import miniMSA_Version2 as MSA

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/run')
def run_MSA():
    return MSA.run()

@app.route('/stop')
def stop_msa():
    return MSA.stop()

if __name__ == "__main__":
   app.run(host="0.0.0.0", port=80, debug=True)

My html code:

<!doctype html>

<head>
<title> MSA Webpage </title>
<meta charset=utf-8>
</head>

    <body>
       <h1> heading </h1>
       <h2> 2nd heading </h2>
       <p> cool paragraph </p>
       <button> <a href="/run"> Start </a> </button>
       <button> <a href="/stop"> Stop </a> </button>
    </body>

External Python script (Note: This is a dumbed down version of my external script, it's over 170 lines long and I have no way to copy and paste it as my Raspberry pi doesn't have internet, so yes I copied all of the code in my post by hand):

import some stuff
import some more stuff

def run():
#create output file name etc

def get_data():
# run, collect and append data to output file

while True:
#flash led code here
# call get_data() function
  • Can you show the code behind`MSA.stop()`? – Dave W. Smith Apr 03 '21 at 03:16
  • I've tried two different things for that function. First, I tried using python's exit() function to just kill the script. The second thing I tried was having my while loop only run when a "running" variable was true, I used the stop function to set that variable to false. Neither of those worked – Zachary Kennedy Apr 03 '21 at 03:28
  • I can't say for sure but, I don't think the start button is working how you think it is right now either. as soon as python gets to that loop it'll freeze up your whole app cause its all running in the same process.. – MRxParkour Apr 03 '21 at 03:29
  • @MRxParkour Ah okay, that's definitely possible. I'm not super experienced. What do you suggest? – Zachary Kennedy Apr 03 '21 at 03:32

2 Answers2

1

alternative.. make it a daemon. allowing you to do os system calls like

python program.py stop
python program.py start
python program.py restart

some related info.

Installing my Python program as a service in Ubuntu

https://stackoverflow.com/a/21597436/14529821

MRxParkour
  • 111
  • 4
0

Suggestion that should solve the problem, but may take some rearranging.

  1. don't import the MSA stuff into the flask app. rather. call the MSA stuff via os calls.

    os.system('./MSA_script.py argument &')

notice the '&' which makes that run in the background (another/new process)

  1. have MSA script occasionally check a file that a file exists. "STOP.txt". you don't even need it to contain anything. if it exists stop the loop

  2. the stop call in flask can just create that STOP.txt file.

  3. you'll also probably want to delete that STOP.txt file before closing, or right before starting the script.

maybe a weird way to do it? idk. but I think it'd work

MRxParkour
  • 111
  • 4