问题描述
在GAE中,您可以说以获取当前登录的用户隐式的当前请求。即使同时处理多个请求,这也是有效的 - users
模块以某种方式知道哪个请求 get_current_user
函数正在为此致电。我查看了开发服务器中模块的代码,它似乎使用 os.environ
来获取与当前请求关联的用户电子邮件和其他值。
这是否意味着每个请求都得到一个独立的 os.environ
对象?
我需要实现类似于 users.get_current_user()
的服务,这将根据调用代码处理的请求返回不同的值。假设 os.environ
是要走的路,我怎么知道哪些变量名已被GAE使用(或保留)?
另外,是否有一种方法可以添加一个在每个请求之前被调用的钩子(或事件处理程序)?
正如所述,
This basically means exactly one request is being served at one time within any given process (although, differently from real CGI, one process can be serially reused for multiple requests, one after the other, if it defines main
functions in the various modules to which app.yaml
dispatches). See also this page, and this one for documentation of the environment variables CGI defines and uses.
The hooks App Engine defines are around calls at the RPC layer, not the HTTP requests. To intercept each request before it gets served, you could use app.yaml
to redirect all requests to a single .py
file and perform your interception in that file's main
function before redirecting (or, you could call your hook at the start of the main
in every module you're using app.yaml
to dispatch to).
这篇关于Google App Engine中的请求感知代码 - os.environ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!