0

I made and published a Flask app on pythonanywhere. This is a single file app with data which is updated every 5 minutes. I need to update my dashboard every 5 minutes. After reloading it will update but it isn't possible to reload manually every 5 minutes. Is there any way to reload app.py using a script or changing some settings?

4
  • You can use javascript to reload the page. Is that what you mean? Commented May 7, 2020 at 20:55
  • javascript can reload a web page only...Right? I want to re-run app.py whole file,which is reload variables too. Commented May 7, 2020 at 20:56
  • What exactly are you trying to reload? Commented May 7, 2020 at 20:58
  • in flask there is one file app.py or whatever name we gave is main file which is help to run our flask application. i hosted that app on pythonanywhere which is runing properly but i want to update it after each 5 min that's need to reload full application on server but i didn't get the way to update automatically or self reload option . [link](siddfulzele.pythonanywhere.com/) Commented May 7, 2020 at 21:07

2 Answers 2

1

PythonAnywhere has an API that allows you to reload a website's code -- the help page I linked to explains all of the details of the API, and has instructions on how to set it up. Also, the example code that is provided on the API token setup page is exactly the Python code you would need to reload a website.

So you could write a script to do that, and then use the "Scheduled tasks" feature to run it every five minutes (you'd need to use twelve hourly tasks for that).

However, I'd recommend against writing your code so that the site needs to be reloaded in order to update. It would be better to make it serve up data directly from a database, and then to schedule tasks that run outside the context of the website to update that database every five minutes.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can do that.

simply start the flask app in a new process then prepare a new function which will be executed after 5 min to restart the script

# coding: utf-8

import sys
import subprocess

from threading import Timer
from multiprocessing import Process

from flask import Flask

app = Flask(__name__)

def run():
    # this is a block function so we must run it in a new process
    app.run()

def run_after(p):
    print("## Restarting ....")
    # terminate the process
    p.terminate()

    ## re-run the script. eg (python test.py blah blah)
    args = [sys.executable] + [sys.argv[0]]
    subprocess.call(args)

if __name__ == "__main__":
    # run flask app in a new process
    p = Process(target=run, args=())
    p.start()

    # set a threading timer which will be execute (run_after) function after 5 min
    _timer = Timer(5 * 60, run_after, (p,))
    _timer.start()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.