1

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
kelvinC
  • 51
  • 6
  • 3
    The feature exists to reload the code in development if it was changed. Without a code change it makes less sense. If you are trying to use any side-effect of the reload, like a resource that is reloaded, you might have made a bad design choice at some point. – Klaus D. Jun 17 '22 at 05:28
  • 2
    Maybe you could `touch` the file containing your app? – Mark Setchell Jun 17 '22 at 06:30

1 Answers1

1

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()
Matteo Pasini
  • 1,787
  • 3
  • 13
  • 26