Where is the best place to put one-time and every-time code in GAE/Python?

為{幸葍}努か 提交于 2019-12-12 13:26:00

问题


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:

  1. 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.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!