这可能是一个“Python Web 编程 101”问题,但我对 aeoid 项目 ( http://github.com/Arachnid/aeoid ) 中的一些代码感到困惑。这是代码:
_current_user = None
def get_current_user():
"""Returns the currently logged in user, or None if no user is logged in."""
global _current_user
if not _current_user and 'aeoid.user' in os.environ:
_current_user = User(None, _from_model_key=os.environ['aeoid.user'])
return _current_user
但我的理解是,全局变量是,嗯,全局的!因此,来自不同用户的不同请求可以(可能)访问和更新相同的值,因此需要 session ,以便存储每个用户的非全局变量。那么,在上面的代码中,是什么阻止一个请求相信当前用户是另一个请求设置的用户呢?抱歉,如果这是基本的,这不是我想象的那样。
谢谢
最佳答案
App Engine Python 运行时是单线程的 - 每个运行时实例一次仅处理一个请求。因此,您可以将全局变量用于特定于请求的参数,只要您注意在每个请求开始时重置它们,这样它们就不会将数据从一个请求泄漏到另一个请求。
关于python - 求启示——AppEngine中的全局变量(aeoid.get_current_user()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2482224/