在app.yaml中定义路由与AppEngine中的WSGIApplication中的一个大型映射有关的性能增益吗? [英] Is there a performance gain from defining routes in app.yaml versus one large mapping in a WSGIApplication in AppEngine?
问题描述
场景1
这涉及在 app.yaml
中使用一个网关路由,然后选择 RequestHandler
在 WSGIApplication
中。
app.yaml < h2>
- url:/.*
script:main.py
- 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)
$ b $ if if __name__ =='__main__':
main()
$ c
$ b 情景2:
这涉及到定义两个路径在 app.yaml
中,然后为每个( page1.py
和 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 )
if __name__ =='__main__':
main()
page2.py
from google.appengine.ext import webapp
$ b $ class class Page2(webapp .RequestHandler):
def get(self):
self.response.out.write(Page 2)
application = webapp.WSGIApplication([
('/ page2 /',Page2),
],debug = True)
def main():
wsgiref.handlers.CGIHandl er()。run(application)
if __name__ =='__main__':
main()
问题
每种模式的优点和缺点是什么?其中一个比另一个快得多?
解决方案唯一的性能影响涉及到模块的加载:模块加载到一个实例上当它们第一次被使用时,分割事物需要更少的模块加载来为新实例提供页面。
尽管这很简单,但您可以因为处理程序脚本很容易按需动态加载所需的模块 - 这就是许多常见框架已经做的事情。
通常,app.yaml路由是为路由不同的组件或应用程序之间例如,remote_api和deferred都有自己的处理程序。因此,为您的应用程序定义一个处理所有其他应用程序的单个处理程序是非常合理的。
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.
这篇关于在app.yaml中定义路由与AppEngine中的WSGIApplication中的一个大型映射有关的性能增益吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!