Home > Net >  Is there a way to reload a flask app without code or file changes?
Is there a way to reload a flask app without code or file changes?

Time:06-17

As flask app will reload itself after it detect changes with the debug=True option, I would like to know if there is a way to manually trigger the reload without making any changes to the code and files.

Preferably trigger the reload when a user accesses "url"/reloadFlask

@app.route('/reloadFlask', methods = ['POST'])
def reloadFlask():
    # Some magic here

CodePudding user response:

If you are running your application as a service (for example via systemctl), just call one of the many functions available in python to execute a command.

For example:

import os

@app.route('/reloadFlask', methods = ['POST'])
def reloadFlask():
    os.system("systemctl restart <your_service_name>")

or:

import subprocess

@app.route('/reloadFlask', methods = ['POST'])
def reloadFlask():
    p = subprocess.Popen(["systemctl", "restart", "<your_service_name>"], stdout=subprocess.PIPE)
    out, err = p.communicate()
  • Related