Too slow to reload

I have a Flask application and every time I reload the web app on Python Anywhere, it takes way too long (something like 10 minutes). After it finally loads, the performance is ok. But the same phenomenon happens again when I make a new request after a period of inactivity (presumably because Python Anywhere is temporarily deactivating the instance). I ran my app on the virtual env, and it shouldn't take more than 30s to load it, so it doesn't seem to be a problem with the code. What is going on?

You can log timestamps of what happens when your web app starts. That would tell you what takes that time.

Yes, I already did that. I put timestamps and executed it in the virtual environment. It is supposed to start running in less than 30s; instead, it is taking 10 min.

The next step would be to find out what part of the site startup is taking up all of that time; if you use print statements like this in the parts of your code that are executed while it is starting up:

print(f"{} about to do X", flush=True)

...then in your server log you will see timestamped messages that will allow you to track down which part of your coe is running slowly. Then we might be able to suggest ways for you to make it faster.

So, after a lot of debugging, it seems that the problem comes from the listen function of Firebase. Every time I do something like


It stops working. The listen function basically listens to every change on the database and updates the data. Locally, it works fine. But on Pythonanywhere, it appears to not load correctly. Any suggestions?

Is it part of your web app startup code?


Is there a specific reason why that code is in the website's code rather than running as (say) an always-on task? If it's listening to Firebase and updating your MySQL or Postgres database, then it would probably be better if you ran it that way; there are multiple separate processes handling requests to your website, so if they were all listening to Firebase and applying updates based on what they received, they could potentially interfere with each other.

I'll try to give more informations. I am using Flask strictly as an API (I send data to it and expect some manipulation in return), and I need to have access to my database in order to fulfill this function. I have two options, then: for each call, I download the data I need from the database and execute my function OR I keep an updated version of my data in memory. The second is what I aim to do, and this is the reason I must use the listen property (which is the main feature of Firebase).

hmm, I think that may lead to some issues- you may have multiple web workers all writing to the same variation at the same time, or stuff that is changed in one web worker may not be changed in another. So you may want to access the db every time. Alternatively, make extra sure that that data doesn't ever change

So it is not possible to have a realtime database working on Python Anywhere? This sounds like a major drawback.

It's certainly possible, but not in memory. In order to handle a non-trivial number of requests to a site, wherever it is deployed, you need to have multiple worker processes. If you're updating information in memory in each of them, then things will get out of sync and you'll get weird responses.

So the normal way to handle that in websites in general is to keep the shared data in a database, and have the website processes access that database.