奇怪的错误:AssertionError:未设置请求全局变量 [英] Strange error: AssertionError: Request global variable is not set

查看:1267
本文介绍了奇怪的错误:AssertionError:未设置请求全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



堆栈跟踪的重要部分

 请求全局变量未设置。 
Traceback(最近一次调用最后):
文件/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第1536行,在__call__
rv = self.handle_exception(request,response,e)
文件/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,行1530,在__call__
rv = self.router.dispatch(request,response)
文件/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,行1278,在default_dispatcher
return route中。 handler_adapter(request,response)
文件/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第1102行,在__call__
return handler.dispatch()
文件/base/data/home/apps/s~kobstadendev/1.359392875892326983/main.py,第81行,发送
webapp2.RequestHandler.dispatch(self)
文件/ base /python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第572行,发送
返回self.handle_exception(e,self.app.debug)
文件/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第570行,发送
返回方法(* args,** kwargs)
文件/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py,行1009,位于synctasklet_wrapper
返回taskletfunc(* args,** kwds).get_result()
文件/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py,第322行,在get_result
self.check_success()
文件/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py,第362行,在_help_tasklet_along
中value = gen.send(val)
文件/base/data/home/apps/s~kobstadendev/1.359392875892326983/items_ndb/items.py,第439行,获取
user = self.auth .get_user_by_session()
文件/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第726行,在__get__
中value = self.func(obj)
文件/base/data/home/apps/s~kobstadendev/1.359392875892326983/main.py,第88行,在auth
返回auth.get_auth()
文件/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2_extras/auth.py,第623行,在get_auth
request = request或webapp2.get_request()
文件/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py,第1720行,在get_request
中断言getattr(_local,'request',None)不是无,_get_request_error
AssertionError:未设置请求全局变量。

加载新实例似乎没有什么用。因此,当一个处理程序被加载并尝试使用时,您可以使用

user = self.auth.get_user_by_session()



webapp2没有正确加载,因为如果我刷新并点击相同的实例,错误消失。有没有人看到这个错误,任何帮助或方向将大大apreciated。



编辑:

多一点信息。我使用

 库:
- 名称:webapp2
版本:latest

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

  class BaseHandler(webapp2.RequestHandler):

BaseHandler for所有请求

持有auth和session属性,以便它们可以被所有请求访问

def dispatch(self):
#获取会话存储这个请求。
self.session_store = sessions.get_store(request = self.request)
try:
#发送请求。
webapp2.RequestHandler.dispatch(self)
finally:
#保存所有会话。
self.session_store.save_sessions(self.response)

@ webapp2.cached_property
def auth(self):
return auth.get_auth()

@ webapp2.cached_property
def session(self):
#使用默认cookie密钥返回一个会话。
返回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):
返回self.auth.get_user_by_session()

app = ndb.toplevel(webapp2.WSGIApplication(routes.URLS,debug = config.DEBUG,config = config.CONFIG))






webapp2出错了吗?

解决方案

这似乎是Webapp2库中的一个Bug,如下所述: https://groups.google.com/forum/?fromgroups=#!topic/webapp2/sHb2RYxG DLc


Im getting really frustrated with this error. After i updated to sdk 1.6.6 im starting to see the following error the first time i run certain handlers.

AssertionError: Request global variable is not set.

the important part of the stacktrace

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.

It seems like something isnt going right when loading up a new instance. So that when a handler gets loaded and try to use

user = self.auth.get_user_by_session()

webapp2 isnt loaded properly, because if i refresh and hit the same instance the error goes away. Has anyone seen this error, any help or direction would be greatly apreciated.

EDIT:

A little more info. Im using

libraries:
- name: webapp2
  version: latest

and python 2.7 . my BaseHandler looks like this

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))


Something is wrong with webapp2 ?

解决方案

This seems to be a Bug in the Webapp2 library as discussed here: https://groups.google.com/forum/?fromgroups=#!topic/webapp2/sHb2RYxGDLc

这篇关于奇怪的错误:AssertionError:未设置请求全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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