Skip to main content
 首页 » 编程设计

python之奇怪的错误 : AssertionError: Request global variable is not set

2025年05月04日10傻小

我对这个错误感到非常沮丧。在我更新到 sdk 1.6.6 后,我第一次运行某些处理程序时开始看到以下错误。

AssertionError:未设置请求全局变量。

堆栈跟踪的重要部分

Request global variable is not set. 
Traceback (most recent call last): 
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__ 
    rv = self.handle_exception(request, response, e) 
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__ 
    rv = self.router.dispatch(request, response) 
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
  File "/base/data/home/apps/s~kobstadendev/1.359392875892326983/main.py", line 81, in dispatch 
    webapp2.RequestHandler.dispatch(self) 
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch 
    return self.handle_exception(e, self.app.debug) 
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 1009, in synctasklet_wrapper 
    return taskletfunc(*args, **kwds).get_result() 
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 322, in get_result 
    self.check_success() 
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 362, in _help_tasklet_along 
    value = gen.send(val) 
  File "/base/data/home/apps/s~kobstadendev/1.359392875892326983/items_ndb/items.py", line 439, in get 
    user = self.auth.get_user_by_session() 
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 726, in __get__ 
    value = self.func(obj) 
  File "/base/data/home/apps/s~kobstadendev/1.359392875892326983/main.py", line 88, in auth 
    return auth.get_auth() 
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2_extras/auth.py", line 623, in get_auth 
    request = request or webapp2.get_request() 
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1720, in get_request 
    assert getattr(_local, 'request', None) is not None, _get_request_error 
AssertionError: Request global variable is not set. 

加载新实例时似乎出现了问题。这样当处理程序被加载并尝试使用

user = self.auth.get_user_by_session()

webapp2 没有正确加载,因为如果我刷新并点击同一个实例,错误就会消失。有没有人看到此错误,将不胜感激任何帮助或指导。

编辑:

更多信息。我正在使用

libraries: 
- name: webapp2 
  version: latest 

和 python 2.7。我的 BaseHandler 看起来像这样

class BaseHandler(webapp2.RequestHandler): 
""" 
    BaseHandler for all requests 
 
    Holds the auth and session properties so they are reachable for all requests 
""" 
def dispatch(self): 
    # Get a session store for this request. 
    self.session_store = sessions.get_store(request=self.request) 
    try: 
        # Dispatch the request. 
        webapp2.RequestHandler.dispatch(self) 
    finally: 
        # Save all sessions. 
        self.session_store.save_sessions(self.response) 
 
@webapp2.cached_property 
def auth(self): 
    return auth.get_auth() 
 
@webapp2.cached_property 
def session(self): 
    # Returns a session using the default cookie key. 
    return self.session_store.get_session() 
 
@webapp2.cached_property 
def session_store(self): 
    return sessions.get_store(request=self.request) 
 
@webapp2.cached_property 
def messages(self): 
    return self.session.get_flashes(key='_messages') 
 
def add_message(self, message, level=None): 
    self.session.add_flash(message, level, key='_messages') 
 
@webapp2.cached_property 
def user(self): 
    return self.auth.get_user_by_session() 
 
app = ndb.toplevel(webapp2.WSGIApplication(routes.URLS, debug=config.DEBUG, config=config.CONFIG)) 

webapp2 有问题吗?

请您参考如下方法:

这似乎是此处讨论的 Webapp2 库中的错误:https://groups.google.com/forum/?fromgroups=#!topic/webapp2/sHb2RYxGDLc