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

问题描述

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

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

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


Scenario 2:

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

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


解决方案

唯一的性能影响与模块的加载有关:模块在第一次使用时加载到实例上,拆分事物需要更少的模块加载到在新实例上提供页面.

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.

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

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.

相关文章