Is there a performance gain from defining routes in app.yaml versus one large mapping in a WSGIApplication in AppEngine?(在 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.py
和 page2.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.
这篇关于在 app.yaml 中定义路由与在 AppEngine 中的 WSGIApplication 中定义一个大型映射相比,是否有性能提升?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 app.yaml 中定义路由与在 AppEngine 中的 WSGIApplication 中定义一个大型映射相比,是否有性能提升?
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01