Running multiple functions without closing Flask server

Viewed 274

I have a Flask app that takes in user input and then passes that input along to other functions in the program. My problem is that the other functions will not run unless I close out of the Flask and shut down the server. How would I keep the server up and call the functions? Essentially once the user hits 'submit' I want all the functions to run.

Starting the flask app

app.run(host = '127.0.0.1', threaded = True)

Passing variables into HTML form input

@app.route('/server_app', methods=["POST"])
def server_app():
    global set_freq, set_center_freq
    set_freq = request.values.get("set_freq")  # <--- do whatever you want with that value
    set_center_freq = request.form.get("set_center_freq")
    return render_template("main_page.html")

Test function is then called, all it does for now is print one of the variables, but it won't run until I use control c and close out of the server.


    def test_fun(set_freq):
        print(set_freq)

    #main
    app.run(host = '127.0.0.1', threaded=True)
    test_fun(set_freq)

it only runs 'test_fun' once I manually stop the flask server from running.

1 Answers

nice to meet you. If you want to run a function you should just be able to define it and run it using def and then run it when you get the data. If there is something that isn't working can you update your question with that specifically. For example what function are you trying to run? So here's some code to go along with it

#Dependencies
from flask import Flask,render_template,session,redirect,url_for,jsonify,send_from_directory


# App stuff
app = Flask(__name__)
def test_fun(set_freq):
    print(set_freq)
#Routers
@app.route("/",methods=["GET"])
def home():
    return render_template("index.html")

@app.route('/server_app', methods=["POST"])
def server_app():
    global set_freq, set_center_freq
    set_freq = request.values.get("set_freq")  # <--- do whatever you want with that value
    set_center_freq = request.form.get("set_center_freq")
    test_fun(set_freq)
    return render_template("main_page.html")



#Run
if __name__ == "__main__":
    app.run(host="0.0.0.0",port=5000)
Related