问题
I am new to Google App Engine and Python. I am having trouble understanding some basic questions about Python apps running on Google App Engine.
If I want code to execute:
- On every incoming request, where should I put it? We are capturing session information about what pages are viewed when by whom, and what did they do, etc.
- Only once when the app is started, where should I put it? I need to initialize a number of application/system variables that are used in many places in the application. Where is the best place to put the code to do this?
If anyone can point me to any documentation or tutorials that explain what the best architecture practices are for GAE/Python apps, without the basics of programming stuff, that would be great.
回答1:
Question Number 1:
Some web frameworks (Django, KAY, etc) have a concept of Middleware. You can create your own middleware that will execute on every request and handle this sort of information (see: https://docs.djangoproject.com/en/dev/topics/http/middleware/)
Question Number 2:
Warmup requests (see: https://developers.google.com/appengine/docs/python/config/appconfig#Warmup_Requests)
Though since warmup requests are not guaranteed to run, you can put a global variable to let the instance know if it was "initialized" and check that variable on every page load (this will be cheap since the variable will live in memory and exist between requests). If it is not set, then run through your "warmup" as needed.
来源:https://stackoverflow.com/questions/12222471/where-is-the-best-place-to-put-one-time-and-every-time-code-in-gae-python