Forums

Automatic reloading of web server (flask app)

I have a simple flask app running on pythonanywhere, and I'd like to make sure it is indeed always running. I set it up about 2 weeks ago, and today I noticed I had to restart the web server for it to work again. Is this typical?

Does it "sleep" if it's been inactive for an extended period of time?

Is there a way to ensure that it reloads if for some reason it isn't running?

I've done some searches on the forums and come up with some example bash scripts to reload the web app, but will these bash scripts get automatically run by pythonanywhere?

That's not normal, no. If your web app has no hits for 26 hours, it will go to sleep -- but that just means that if it gets a hit, it will be automatically started up again, so the first hit after a long break might be processed a little slowly. (Exactly how slowly depends on how long your code takes to start up.)

When you say that you had to restart the web server for it to work again, what were the symptoms of it not working?

Ok, good to know that it's supposed to keep on ticking.

Looking through the server logs I see a bunch of start up related stuff. The whole start up process seems to have taken less than 1 second (given the time stamps of the server logs).

I'm using flask to process a twilio sms request. Before I restarted the flask app finished having read the msg as None. After the restart, I sent a new twilio request and everything worked as intended (e.g. it read a message and responded).

Is it possible that because the app had to be restarted, it somehow missed the message contents? Any suggestions on how to solve that?

Hmm, that's possible. I can see a code path where for the first hit to a website when it's being woken up, POST (and maybe GET) data might be lost.

Perhaps the best thing would be to add a scheduled task that just wgets the root page of the app once a day? That would stop it from sleeping. That said, you'd still potentially have a problem if the server the app is running on gets rebooted, as your app would then not be running until the first hit.