Python 何时以及如何使用 Tornado?什么时候没用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4212877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When and how to use Tornado? When is it useless?
提问by Vladimir Sidorenko
Ok, Tornado is non-blocking and quite fast and it can handle a lot of standing requests easily.
好的,Tornado 是非阻塞的并且速度非常快,它可以轻松处理大量的长期请求。
But I guess it's not a silver bullet and if we just blindly run Django-based or any other site with Tornado it won't give any performance boost.
但我想这不是灵丹妙药,如果我们只是盲目地运行基于 Django 的站点或任何其他带有 Tornado 的站点,它不会带来任何性能提升。
I couldn't find comprehensive explanation of this, so I'm asking it here:
我找不到对此的全面解释,所以我在这里问:
- When should Tornado be used?
- When is it useless?
- When using it, what should be taken into account?
- How can we make inefficientsite using Tornado?
- There is a server and a webframework. When should we use framework and when can we replace it with other one?
- 什么时候应该使用 Tornado?
- 什么时候没用?
- 使用时,应该注意什么?
- 我们如何使用 Tornado制作低效的网站?
- 有一个服务器和一个网络框架。我们什么时候应该使用框架,什么时候可以用其他框架替换它?
采纳答案by pyfunc
There is a server and a webframework. When should we use framework and when can we replace it with other one?
有一个服务器和一个网络框架。我们什么时候应该使用框架,什么时候可以用其他框架替换它?
This distinction is a bit blurry. If you are only serving static pages, you would use one of the fast servers like lighthttpd. Otherwise, most servers provide a varying complexity of framework to develop web applications. Tornado is a good web framework. Twisted is even more capable and is considered a good networking framework. It has support for lot of protocols.
这个区别有点模糊。如果您只提供静态页面,则可以使用一种快速服务器,例如 lighthttpd。否则,大多数服务器会提供不同复杂度的框架来开发 Web 应用程序。Tornado 是一个很好的网络框架。Twisted 更强大,被认为是一个很好的网络框架。它支持许多协议。
Tornado and Twisted are frameworks that provide support non-blocking, asynchronous web / networking application development.
Tornado 和 Twisted 是提供支持非阻塞、异步 Web / 网络应用程序开发的框架。
When should Tornado be used? When is it useless? When using it, what should be taken into account?
什么时候应该使用 Tornado?什么时候没用?使用时,应该注意什么?
By its very nature, Async / Non-Blocking I/O works great when it is I/O intensive and not computation intensive. Most web / networking applications suits well for this model. If your application demands certain computational intensive task to be done then it has to be delegated to some other service that can handle it better. While Tornado / Twisted can do the job of web server, responding to web requests.
就其本质而言,异步/非阻塞 I/O 在 I/O 密集型而非计算密集型时效果很好。大多数 Web / 网络应用程序都非常适合此模型。如果您的应用程序需要完成某些计算密集型任务,则必须将其委托给可以更好地处理它的其他服务。Tornado / Twisted 可以完成 Web 服务器的工作,响应 Web 请求。
How can we make inefficient site using Tornado?
我们如何使用 Tornado 制作低效的网站?
- Do any thing computational intensive task
- Introduce blocking operations
- 做任何计算密集型任务
- 引入阻塞操作
But I guess it's not a silver bullet and if we just blindly run Django-based or any other site with Tornado it won't give any performance boost.
但我想这不是灵丹妙药,如果我们只是盲目地运行基于 Django 的站点或任何其他带有 Tornado 的站点,它不会带来任何性能提升。
Performance is usually a characteristic of complete web application architecture. You can bring down the performance with most web frameworks, if the application is not designed properly. Think about caching, load balancing etc.
性能通常是完整的 Web 应用程序架构的特征。如果应用程序设计不当,您可能会降低大多数 Web 框架的性能。考虑缓存、负载平衡等。
Tornado and Twisted provide reasonable performance and they are good for building performant web applications. You can check out the testimonials for both twisted and tornado to see what they are capable of.
Tornado 和 Twisted 提供了合理的性能,它们非常适合构建高性能的 Web 应用程序。您可以查看扭曲和龙卷风的推荐信,看看它们的能力。
回答by Morten Jensen
I'm sorry for answering an old question, but I came across this one and wondered why it didn't have more answers. To answer Bart J's question:
很抱歉回答了一个老问题,但我遇到了这个问题,想知道为什么它没有更多答案。回答巴特 J 的问题:
I would like to parse RSS feeds in the Tornado application. Would you consider that fairly computationally intensive?
我想在 Tornado 应用程序中解析 RSS 提要。你会认为这是相当密集的计算吗?
Well that depends on what kind of parsing you're doing and on what hardware :) Long time is a long time, so if your app takes more than say half a second to respond, it'll seem sluggish - profile your app.
嗯,这取决于你正在做什么样的解析以及什么硬件:) 长时间是很长的时间,所以如果你的应用程序需要超过半秒的时间来响应,它看起来会很慢 - 配置你的应用程序。
The key to fast systems is great architecture, not so much the specifics as for instance which framework you're using (Twisted, Tornado, Apache+PHP). Tornado has an asynchronous processing style and that's really what a lot of it comes down to in my opinion. Node.js, Twisted and Yaws are examples of other asynchronous web servers that scale very well because of a lightweight approach and asynchronous processing style.
快速系统的关键是出色的架构,而不是具体细节,例如您使用的是哪种框架(Twisted、Tornado、Apache+PHP)。Tornado 具有异步处理风格,在我看来,这确实是很多原因。Node.js、Twisted 和 Yaws 是其他异步 Web 服务器的示例,它们由于采用轻量级方法和异步处理方式而具有很好的可扩展性。
So:
所以:
When should Tornado be used?
When is it useless?
什么时候应该使用 Tornado?
什么时候没用?
Tornado is good for handling a lot of connections, since it can respond to an incoming client, dispatch a request handler and don't think about that client until the result-callback is pushed on the event queue. So for that specific quality Tornado should be used when you want to scale well when handling a lot of requests. The async processing facilitates functional decoupling and shared-nothing data access. That swings really well with stateless design like RESTor other Service Oriented Architectures. You also don't have to deal with spawning threads or processes with the inherent overhead so much and you can save some of the locking/IPC trouble.
Tornado 非常适合处理大量连接,因为它可以响应传入的客户端,分派请求处理程序,并且在结果回调被推送到事件队列之前不考虑该客户端。因此,当您想在处理大量请求时很好地扩展时,应该使用特定质量的 Tornado。异步处理有助于功能解耦和无共享数据访问。这对于无状态设计(如REST或其他面向服务的架构)非常有效。您也不必处理具有太多固有开销的生成线程或进程,并且可以省去一些锁定/IPC 麻烦。
Tornado won't make much of a difference, on the other hand, if your backend and/or data store takes a long time to process the requests. It helps to do concurrent designs and Web services in particular. The concurrent architecture makes it easier to scale your design and keep the coupling low. That's my experience with Tornado at least.
另一方面,如果您的后端和/或数据存储需要很长时间来处理请求,Tornado 不会产生太大影响。它特别有助于进行并发设计和 Web 服务。并发架构可以更轻松地扩展您的设计并保持低耦合。这至少是我对 Tornado 的体验。

