问题
I'm running a Django-based web app on Google App Engine under the Python 3.7 Standard Environment. When using the app, requests usually take around 500ms, which is completely acceptable. Howevever, when the app has not been accessed for some time (a few minutes), the logs show that the Google App Engine instance is shut down, and the next request requires gunicorn to load again and takes about 20 second.
I obciously can't have users wait 20 seconds before the page loads. When testing on my local setup, the server takes a few seconds to load environment variables, and then the debug server loads almost immediately.
I don't think there is a problem with my code, given that once the "cold start" occurs, everything is running fast, so it's not like the requests are waiting for a database read or something like that.
What options are there to optimise django cold starts on Google App Engine?
So far, I've increased instance class to F4, and specified the number fo gunicorn workers according to this guide. I can in theory go to F4_1G, but that's the highest available instance, and it doesn't seem to address the cold start issue.
The only other thing I can think of that could slow down the instance start up is that in my app.yaml, I have 32 environment variables set up (mostly API credentials). Could this be the main reason for the long start up times? And if so, is there a secure alternative of providing API credentials to Django without using environment variables?
Thank you
回答1:
Comparing performance on GAE vs local machines isn't really relevant, see Why does Google Cloud SQL (using JDBC) take longer to insert records from Google App Engine than from my personal computer?
Those seconds you see locally aren't spent just loading the local variables (you can actually measure it and display it in a log message if you're not convinced), most of it is spent importing all the needed libraries and setting up the django framework (and maybe something else for your app), you'd need to profile it to figure out exactly what's going on and if/what can be done to significantly improve it.
The typical ways of minimizing the impact of cold start times are:
- scalability configs - have a minimum instance running at all times
- warmup requests (but they're not 100% effective)
来源:https://stackoverflow.com/questions/57054132/improving-cold-start-up-times-on-google-app-engine-running-django-on-python-3-7