Google App Engine中的请求感知代码 - os.environ? [英] Request-Aware Code in Google App Engine -- os.environ?

查看:116
本文介绍了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使用(或保留)?



另外,是否有一种方法可以添加一个在每个请求之前被调用的钩子(或事件处理程序)?

解决方案

正如文档所述,


Python web应用程序使用CGI
协议与
App Engine Web服务器交互。


blockquote>

这基本上意味着在任意给定的进程中一次提供一个请求(尽管与真实CGI不同,一个进程可以连续地重复用于多个请求,其他,如果它定义 app.yaml 分派的各个模块中的 main 函数)。另请参阅此页,以及这一个用于CGI定义和使用的环境变量文档。



App Engine定义的钩子是在RPC层调用的,而不是HTTP请求。为了拦截每个请求,可以使用 app.yaml 将所有请求重定向到一个 .py 文件并在重定向之前在该文件的 main 函数中执行拦截(或者,您可以在 main 在你使用的每个模块中 app.yaml 分派到)。


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?

解决方案

As the docs say,

A Python web app interacts with the App Engine web server using the CGI protocol.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆