开发者

Is there a performance gain from defining routes in app.yaml versus one large mapping in a WSGIApplication in AppEngine?

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜