在重定向之后,如何在GAE上尽早关闭Python web应用程序?

在重定向之后,如何在GAE上尽早关闭Python web应用程序?,python,google-app-engine,web-applications,Python,Google App Engine,Web Applications,免责声明:Python在PHP背景中是全新的 好的,我正在谷歌应用程序引擎上使用Python和谷歌的webapp框架 我有一个我导入的函数,因为它包含需要在每个页面上处理的内容 def some_function(self): if data['user'].new_user and not self.request.path == '/main/new': self.redirect('/main/new') 当我调用它时,它可以正常工作,但如何确保在重定向后关闭应用

免责声明:Python在PHP背景中是全新的

好的,我正在谷歌应用程序引擎上使用Python和谷歌的webapp框架

我有一个我导入的函数,因为它包含需要在每个页面上处理的内容

def some_function(self):
    if data['user'].new_user and not self.request.path == '/main/new':
        self.redirect('/main/new')
当我调用它时,它可以正常工作,但如何确保在重定向后关闭应用程序。我不需要任何其他处理。例如,我将这样做:

class Dashboard(webapp.RequestHandler):
    def get(self):
        some_function(self)
        #Continue with normal code here
        self.response.out.write('Some output here')
我想确保,一旦在某个函数()中进行了重定向(这很好),重定向后get()函数中不会进行任何处理,也不会输出“some output here”

我应该看什么才能让这一切正常工作?我不能退出脚本,因为webapp框架需要运行

我意识到,对于Python应用程序来说,我很可能只是在以完全错误的方式做事,所以任何指导都会有很大帮助。希望我已经正确地解释了自己,有人能给我指出正确的方向


谢谢

我建议您根据调用方是否应该继续执行,从
some_function()
返回一个布尔值。例如:

def some_function(self):
    if data['user'].new_user and not self.request.path == '/main/new':
        self.redirect('/main/new')
        return True
    return False

class Dashboard(webapp.RequestHandler):
    def get(self):
        if some_function(self):
            return
        #Continue with normal code here
        self.response.out.write('Some output here')

如果
some_function()
嵌套了几层,或者您可能有许多这样的函数,那么还有一个稍微复杂一点的替代方法可能会有所帮助。其思想是:引发一个异常,指示您希望停止处理,并使用
webapp.RequestHandler
的子类,该子类只捕获并忽略此异常。下面是一个大概的情况:

class RedirectException(Exception):
    """Raise this from any method on a MyRequestHandler object to redirect immediately."""
    def __init__(self, uri, permanent=False):
        self.uri = uri
        self.permanent = permanent

class RedirectRequestHandler(webapp.RequestHandler):
    def handle_exception(self, exception, debug_mode):
        if isinstance(exception, RedirectException):
            self.redirect(exception.uri, exception.permanent)
        else:
            super(MyRequestHandler, self).handle_exception(exception, debug_mode)
这可能会使使用
某些函数()
更容易一些(并使您的其他请求处理程序更容易阅读)。例如:

def some_function(self):
    if data['user'].new_user and not self.request.path == '/main/new':
        raise RedirectException('/main/new')

class Dashboard(RedirectRequestHandler):
     # rest of the implementation is the same ...
这个怎么样

class Dashboard(webapp.RequestHandler):
    def some_function(self):
        if data['user'].new_user and not self.request.path == '/main/new':
            self.redirect('/main/new')
            return True
        else:
            return False
    def get(self):
        if not self.some_function():
            self.response.out.write('Some output here')
作为参考,如果您需要在许多RequestHandler中使用一些函数(),那么创建一个您的其他RequestHandler可以从中生成子类的类将是pythonic的:

class BaseHandler(webapp.RequestHandler):
    def some_function(self):
        if data['user'].new_user and not self.request.path == '/main/new':
            self.redirect('/main/new')
            return False
        else:
            return True

class Dashboard(BaseHandler):
    def get(self):
        if not self.some_function():
            self.response.out.write('Some output here')

我知道这个问题已经很老了,但我今天做了一些事情,自然地尝试了另一个解决方案,没有考虑它,它工作得很好,但我想知道这样做是否会有问题

我现在的解决方案就是返回,实际上返回任何东西,但我使用了“returnfalse”,因为请求本身存在问题,所以我正在打印错误或重定向到其他地方

通过返回,我已经设置了输出等,并且我提前终止了get()或post()函数


这是一个不错的解决方案吗?

两个很好的解决方案,第二个解决方案随着我的学习进步让我的思维有了一点开阔。返回布尔值是我一直在做的事情,但是你已经去帮我整理了一下-非常感谢你的回答。说实话,我只是在10分钟后才开始了解异常,它开始变得更清晰了,但我认为它仍然在我的脑海里。不过,我会研究一下,非常感谢。布尔方法当然更简单,应该足以处理简单的情况(如您在问题中提出的情况)。只需在你的后袋中保留异常方法,并考虑当布尔方案变得更棘手(例如嵌套函数或很多函数,如<代码>某个函数()/代码>)。我认为这是任何使用webapp的
RequestHandler.redirect()
的人在第一次使用它时遇到的问题。正如我在另一个答案上所评论的,我一直在返回布尔值,但你让它对我来说更干净了,而且通常让我的思维开阔了一点。我喜欢你关于建立一个RequestHandler可以从中生成子类的类的想法,我将这样做:)感谢你为我指明了成为真正的Python头的正确方向。我在PHP方面有6年的经验。。。在Python中只有几天的时间,要学的东西太多了!我是StackOverflow的新手,这是标记为已接受的答案,因为我将使用布尔部分和关于如何更“pythonic”的第二条建议:)非常感谢你们两位。