Google App Engine 中的请求感知代码 - os.environ?
在 GAE 中,您可以说 users.get_current_user()
获取当前请求隐含的当前登录用户。即使同时处理多个请求,这种方法也能工作——users
模块以某种方式知道 get_current_user
函数正在代表哪个请求被调用。我查看了开发服务器中模块的代码,它似乎使用 os.environ 来获取用户电子邮件和与当前请求关联的其他值。
这是否意味着每个请求都会获得一个独立的 os.environ 对象?
我需要实现一个类似于 users.get_current_user() 的服务,该服务将根据调用代码处理的请求返回不同的值。假设 os.environ 是可行的方法,我如何知道哪些变量名称已被 GAE 使用(或保留)?
另外,有没有办法添加一个在每个请求之前调用的钩子(或事件处理程序)?
In GAE, you can say users.get_current_user()
to get the currently logged-in user implicit to the current request. This works even if multiple requests are being processed simultaneously -- the users
module is somehow aware of which request the get_current_user
function is being called on behalf of. I took a look into the code of the module in the development server, and it seems to be using os.environ
to get the user email and other values associated to the current request.
Does this mean that every request gets an independent os.environ
object?
I need to implement a service similar to users.get_current_user()
that would return different values depending on the request being handled by the calling code. Assuming os.environ
is the way to go, how do I know which variable names are already being used (or reserved) by GAE?
Also, is there a way to add a hook (or event handler) that gets called before every request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如文档所说,
这基本上意味着在任何给定进程中一次只服务一个请求(尽管与真正的 CGI 不同,一个进程可以连续地重用多个请求,一个接一个,如果它定义了
main
app.yaml
分派到的各个模块中的函数)。另请参阅此页面和这个 用于 CGI 定义和使用的环境变量的文档。App Engine 定义的挂钩是围绕 RPC 层的调用,而不是 HTTP 请求。要在提供服务之前拦截每个请求,您可以使用
app.yaml
将所有请求重定向到单个.py
文件,并在该文件的main 中执行拦截重定向之前的
函数(或者,您可以在使用app.yaml
分派到的每个模块中的main
开头调用挂钩)。As the docs say,
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 whichapp.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'smain
function before redirecting (or, you could call your hook at the start of themain
in every module you're usingapp.yaml
to dispatch to).