与 AppEngine 中 WSGIApplication 中的一个大型映射相比,在 app.yaml 中定义路由是否会带来性能提升?

发布于 2024-09-04 22:55:42 字数 1727 浏览 6 评论 0原文

场景 1

这涉及在 app.yaml 中使用一个“网关”路由,然后在 WSGIApplication 中选择 RequestHandler

app.yaml

- url: /.*
  script: main.py

main.py

from google.appengine.ext import webapp

class Page1(webapp.RequestHandler):
    def get(self):
        self.response.out.write("Page 1")

class Page2(webapp.RequestHandler):
    def get(self):
        self.response.out.write("Page 2")

application = webapp.WSGIApplication([
    ('/page1/', Page1),
    ('/page2/', Page2),
], debug=True)

def main():
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()

场景 2:

这涉及在 app.yaml 中定义两个路由,然后为每个路由定义两个单独的脚本(page1.pypage2.py )。

app.yaml

- url: /page1/
  script: page1.py
- url: /page2/
  script: page2.py

page1.py

from google.appengine.ext import webapp

class Page1(webapp.RequestHandler):
    def get(self):
        self.response.out.write("Page 1")

application = webapp.WSGIApplication([
    ('/page1/', Page1),
], debug=True)

def main():
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()

page2.py

from google.appengine.ext import webapp

class Page2(webapp.RequestHandler):
    def get(self):
        self.response.out.write("Page 2")

application = webapp.WSGIApplication([
    ('/page2/', Page2),
], debug=True)

def main():
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()

问题

每种模式的优点和缺点是什么?一个比另一个快很多吗?

Scenario 1

This involves using one "gateway" route in app.yaml and then choosing the RequestHandler in the WSGIApplication.

app.yaml

- url: /.*
  script: main.py

main.py

from google.appengine.ext import webapp

class Page1(webapp.RequestHandler):
    def get(self):
        self.response.out.write("Page 1")

class Page2(webapp.RequestHandler):
    def get(self):
        self.response.out.write("Page 2")

application = webapp.WSGIApplication([
    ('/page1/', Page1),
    ('/page2/', Page2),
], debug=True)

def main():
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()

Scenario 2:

This involves defining two routes in app.yaml and then two separate scripts for each (page1.py and page2.py).

app.yaml

- url: /page1/
  script: page1.py
- url: /page2/
  script: page2.py

page1.py

from google.appengine.ext import webapp

class Page1(webapp.RequestHandler):
    def get(self):
        self.response.out.write("Page 1")

application = webapp.WSGIApplication([
    ('/page1/', Page1),
], debug=True)

def main():
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()

page2.py

from google.appengine.ext import webapp

class Page2(webapp.RequestHandler):
    def get(self):
        self.response.out.write("Page 2")

application = webapp.WSGIApplication([
    ('/page2/', Page2),
], debug=True)

def main():
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()

Question

What are the benefits and drawbacks of each pattern? Is one much faster than the other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朦胧时间 2024-09-11 22:55:42

唯一的性能影响与模块的加载有关:模块在第一次使用时加载到实例上,并且拆分模块需要更少的模块加载来为新实例上的页面提供服务。

不过,这是非常小的,因为您可以轻松地让处理程序脚本按需动态加载所需的模块 - 这就是许多常见框架已经做的事情。

一般来说,app.yaml 路由设计用于不同组件或应用程序之间的路由。例如,remote_api 和 deferred 都有自己的处理程序。因此,为您的应用程序定义一个处理程序来处理其他所有事情是完全合理的。

The only performance implication relates to the loading of modules: Modules are loaded on an instance when they're first used, and splitting things up requires fewer module loads to serve a page on a new instance.

This is pretty minimal, though, as you can just as easily have the handler script dynamically load the needed module on-demand - and that's what many common frameworks already do.

In general, app.yaml routing is designed for routing between distinct components or applications. For example, remote_api and deferred both have their own handlers. It's perfectly reasonable, therefore, to have a single handler defined for your app that handles everything else.

静赏你的温柔 2024-09-11 22:55:42

我不认为这对性能有任何影响,但根据功能将应用程序拆分为文件将帮助您更好地管理它,特别是当它由多人开发时。

例如,所有与查看、编辑、删除等页面有关的处理程序都可以在 pages.py 中,而所有与查看等、用户配置文件有关的处理程序可以在 pages.py 中。在 user_profiles.py 中,所有与 JSON REST API 相关的处理程序都可以在 rest_api.py 中,依此类推。

但再次强调,我不认为这会对运行时性能产生任何影响,而只会对开发时性能产生影响。

I don't believe there's any performance implication, but splitting your app into files based on functionality will help you manage it better, especially if it's being developed by multiple people.

For example, all handlers that have to do with viewing, editing, deleting, etc., pages could be in pages.py while all handlers that have to do with viewing, etc., user profiles could be in user_profiles.py, and all handlers having to do with a JSON REST API could be in rest_api.py, and so on.

But again, I don't believe this has any runtime performance implication, just a development-time performance implication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文