Python 何时使用 Tornado,何时使用 Twisted / Cyclone / GEvent / other
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13941903/
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 to use Tornado, when to use Twisted / Cyclone / GEvent / other
提问by Wojciech Danilo
Which of these frameworks / libraries would be the best choise for building modern multiuser web application? I would love to have an asynchronous webserver which will allow me to scale easly. What solution will give the best performance/ scalability/ most useful framework(in terms of easy of use and easy of developing)?
这些框架/库中的哪一个是构建现代多用户 Web 应用程序的最佳选择?我很想拥有一个异步网络服务器,它可以让我轻松扩展。什么解决方案将提供最佳性能/可扩展性/最有用的框架(在易于使用和易于开发方面)?
It would be great if it will provide good functionality(websockets, rpc, streaming, etc).
如果它能提供良好的功能(websockets、rpc、流等),那就太好了。
What are the pros and cons of each solution?
每种解决方案的优缺点是什么?
采纳答案by dhilipsiva
"Djangois a high-level Python Web framework that encourages rapid development and clean, pragmatic design". If you are building something that is similar to a e-commerce site, then you should probably go with Django. It will get your work done quick. You dont have to worry about too many technology choices. It provides everything thing you need from template engine to ORM. It will be slightly opinionated about the way you structure your app, which is good If you ask me. And it has the strongest community of all the other libraries, which means easy help is available.
“ Django是一个高级 Python Web 框架,它鼓励快速开发和简洁实用的设计”。如果您正在构建类似于电子商务网站的内容,那么您可能应该使用 Django。它会让你的工作快速完成。您不必担心太多的技术选择。它提供了从模板引擎到 ORM 所需的一切。对您构建应用程序的方式会有些自以为是,如果您问我,这很好。它拥有所有其他图书馆中最强大的社区,这意味着可以轻松获得帮助。
"Flaskis a microframework for Python based on Werkzeug, Jinja 2 and good intentions". Beware - "microframework" may be misleading. This does not mean that Flask is a half-baked library. This mean the core of flask is very, very simple. Unlike Django, It will not make any Technology decisions for you. You are free to choose any template engine or ORM that pleases you. Even though it comes with Jinja template engine by default, you are always free to choose our own. As far as I know Flask comes in handy for writing APIs endpoints (RESTful services).
“ Flask是基于 Werkzeug、Jinja 2 和良好意图的 Python 微框架”。当心 - “微框架”可能会产生误导。这并不意味着 Flask 是一个半生不熟的库。这意味着烧瓶的核心非常非常简单。与 Django 不同,它不会为您做出任何技术决策。您可以自由选择任何您喜欢的模板引擎或 ORM。尽管它默认带有 Jinja 模板引擎,但您始终可以自由选择我们自己的模板引擎。据我所知,Flask 可用于编写 API 端点(RESTful 服务)。
"Twistedis an event-driven networking engine written in python". This is a high-performance engine. The main reason for its speed is something called as deferred. Twisted is built on top of deferreds. For those of you who dont know about defereds, it is the mechanism through with asynchronous architecture is achieved. Twisted is very fast. But is not suitable for writing conventional webapps. If you want to do something low-level networking stuff, twisted is your friend.
“ Twisted是一个用 Python 编写的事件驱动的网络引擎”。这是一个高性能引擎。其速度的主要原因是所谓的延迟。Twisted 建立在延迟之上。对于那些不了解延迟的人,它是通过异步架构实现的机制。扭曲是非常快的。但不适合编写常规的 webapp。如果你想做一些低级网络的事情,twisted 是你的朋友。
"Tornadois a Python web framework and asynchronous networking library, originally developed at FriendFeed. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user". Tornado stands some where between Django and Flask. If you want to write something with Django or Flask, but if you need a better performance, you can opt for Tornado. it can handle C10k problem very well if it is architected right.
" Tornado是一个 Python Web 框架和异步网络库,最初由 FriendFeed 开发。通过使用非阻塞网络 I/O,Tornado 可以扩展到数万个打开的连接,使其成为长轮询、WebSockets 和其他应用程序的理想选择需要与每个用户建立长期连接”。Tornado 介于 Django 和 Flask 之间。如果你想用 Django 或 Flask 写一些东西,但如果你需要更好的性能,你可以选择 Tornado。如果架构正确,它可以很好地处理 C10k 问题。
"Cycloneis a web server framework for Python that implements the Tornado API as a Twisted protocol". Now, what if you want something that is nearly as performant as Twisted but easy to write conventional webapps? Say hello to cyclone. I would prefer Cyclone over Tornado. It has an API that is very similar to Tornado. As a matter of fact, this is a fork of Tornado. But the problem is it has relativly small community. Alexandre Fiori is the only main commiter to the repo.
“ Cyclone是 Python 的 Web 服务器框架,它将 Tornado API 实现为 Twisted 协议”。现在,如果您想要一些性能几乎与 Twisted 一样但易于编写传统 web 应用程序的东西,该怎么办?向旋风问好。我更喜欢旋风而不是龙卷风。它有一个与 Tornado 非常相似的 API。事实上,这是 Tornado 的一个分支。但问题是它的社区相对较小。Alexandre Fiori 是该回购协议的唯一主要提交者。
"Pyramidis a general, open source, Python web application development framework. Its primary goal is to make it easier for a Python developer to create web applications."I haven't really used Pyramid, but I went through the documentation. From what I understand, Pyramid is very similar to Flaskand I think you can use Pyramid wherever Flaskseems appropriate and vice-versa.
“ Pyramid是一个通用的开源 Python Web 应用程序开发框架。它的主要目标是让 Python 开发人员更容易创建 Web 应用程序。” 我还没有真正使用过 Pyramid,但我浏览了文档。据我了解,Pyramid 与Flask非常相似,我认为您可以在Flask看起来合适的任何地方使用 Pyramid ,反之亦然。
EDIT: Request to review any other frameworks are welcomed!
编辑:欢迎要求任何其他框架!
来源:http: //dhilipsiva.com/2013/05/19/python-libraries-django-twisted-tornado-flask-cyclone-and-pyramid.html
回答by Glyph
This is obviously a somewhat biasedanswer, but that is not the same thing as a wronganswer; you should always use Twisted. I've answered similar questionsbefore, but since your question is not quite the same, here are some reasons:
这显然是一个有些偏颇的答案,但这与错误答案不同;您应该始终使用 Twisted。我之前回答过类似的问题,但由于你的问题不太一样,这里有一些原因:
"Best Performance"
“最棒的表演”
Twisted continuously monitors our performance at the speed.twistedmatrix.comwebsite. We were also one of the first projects to be monitored by PyPy's similar site, thereby assuring the good performance of Twisted on the runtime that anyone concerned with high-performance applications in Python.
Twisted 在speed.twistedmatrix.com网站上持续监控我们的表现。我们也是最早被 PyPy 的类似站点监控的项目之一,从而确保了 Twisted 在运行时的良好性能,任何关心 Python 中的高性能应用程序的人。
"Scalability"
“可扩展性”
To my knowledge, none of the listed frameworks have any built-in support for automatic scaling; they're all communication frameworks, so you have to do the work to communicate between your scaling nodes. However, Twisted has an advantage in its built-in support for local multi-processing. In fairness, there is a third-party add-on for Tornadothat allows you to do the same thing. In recent releases, Twisted has added featuresthat increase the number of ways you can share work between cores, and work is ongoing in that area. Twisted also has a couple of well-integrated, "native"RPC protocols which offer a construction-kit for whatever scaling idiom you want to pursue.
据我所知,列出的框架都没有对自动扩展的任何内置支持;它们都是通信框架,因此您必须完成在扩展节点之间进行通信的工作。但是,Twisted 在其对本地多处理的内置支持方面具有优势。公平地说,Tornado有一个第三方插件可以让你做同样的事情。在最近的版本中,Twisted增加了一些功能,可以增加内核之间共享工作的方式数量,并且该领域的工作正在进行中。扭曲的也有一对夫妇的良好整合,“本土”RPC它提供了一个建筑套件,无论缩放成语你想追求的协议。
"Most Useful"
“最有用”
Lots of peopleseem to find Twisted very useful. So much so that many of them have extended it and made their extensions available to you.
很多人似乎发现 Twisted非常有用。 如此之多以至于他们中的许多人已经扩展了它并使他们的扩展对您可用。
"Functionality"
“功能”
Out of the box, Twisted includes:
开箱即用,Twisted 包括:
- good support for test-driven developmentof all the following
- TCPservers, clients, transport layer security
- SSHclient and server
- IMAP4, ESMTP, POP3clients and servers
- DNSclient and server
- HTTPclient and server
- IRC, XMPP, OSCAR, MSNclients and servers
- 对以下所有测试驱动开发的良好支持
- TCP服务器、客户端、传输层安全
- SSH客户端和服务器
- IMAP4、ESMTP、POP3客户端和服务器
- DNS客户端和服务器
- HTTP客户端和服务器
- IRC、XMPP、OSCAR、MSN客户端和服务器
In this last department, at least, Twisted seems a clear winner for built-in functionality. And all this, in a package just over 2 megabytes!
至少在最后一个部门,Twisted 似乎是内置功能的明显赢家。而这一切,在一个刚刚超过 2 兆字节的包中!
回答by Robert Zaremba
I like @Glyph response. Twisted is very comprehensive, rich python framework. Twisted and Tornado have a very similar design. And I like this design very much:
我喜欢@Glyph 响应。Twisted 是非常全面、丰富的python 框架。Twisted 和 Tornado 的设计非常相似。而且我非常喜欢这个设计:
- it's fast
- easy to understand
- easy to extend
- doesn't require c-extensions
- works on PyPy.
- 它很快
- 容易理解
- 易于扩展
- 不需要c 扩展
- 在 PyPy 上工作。
But I want to highlight Tornado, which I prefer and recently gain popularity.
Tornado, like Twisted, uses callback style programming, but it can be inlined using tornado.gen.engine(twisted.internet.inlineCallbacksin Twisted).
但我想强调Tornado,我更喜欢它并且最近很受欢迎。Tornado 与 Twisted 一样,使用回调式编程,但可以使用tornado.gen.engine(twisted.internet.inlineCallbacks在 Twisted 中)进行内联。
Codebase
代码库
The best comment is from http://cyclone.iosite. cyclonetries to mix Twisted and Tornado because:
最好的评论来自http://cyclone.io站点。气旋试图混合扭曲和龙卷风,因为:
Twisted is one of the most mature libraries for non-blocking I/O available to the public. Tornado is the open source version of FriendFeed's web server, one of the most popular and fast web servers for Python, with a very decent API for building web applications.
The idea is to bridge Tornado's elegant and straightforward API to Twisted's Event-Loop, enabling a vast number of supported protocols.
Twisted 是最成熟的非阻塞 I/O 库之一,可供公众使用。Tornado 是 FriendFeed 网络服务器的开源版本,它是最流行和最快速的 Python 网络服务器之一,具有用于构建网络应用程序的非常体面的 API。
这个想法是将 Tornado 优雅而直接的 API 桥接到 Twisted 的事件循环,从而支持大量支持的协议。
But in 2011 tornado.platform.twistedwas out which brings similar functionality.
但是在 2011tornado.platform.twisted年出来了,它带来了类似的功能。
Performance
表现
Tornado has much better performance. It also works seamlessly with PyPy, and get huge gain.
Tornado 有更好的性能。它还可以与 PyPy 无缝协作,并获得巨大收益。
Scalability
可扩展性
The same like Twisted. Tornado has tornado.processand a lot of rpc services implemented on top of it.
和 Twisted 一样。Tornado 已经tornado.process在它之上实现了许多 rpc 服务。
Functionality
功能
There are 71Tornado based package, compared to 148 Twisted's and 48 Gevent's. But if you look carefully and compute median of packages upload time, you will see that Twisted ones are the oldest, then Gevent and Tornado the freshest.
Furthermore there is tornado.platform.twistedmodule which allows you to run code written for Twisted on Tornado.
与 148 个 Twisted 和 48 个 Gevent 相比,有 71 个基于 Tornado 的软件包。但是如果你仔细观察并计算包上传时间的中位数,你会发现 Twisted 是最老的,然后是 Gevent 和 Tornado 最新鲜。此外,还有一个tornado.platform.twisted模块允许您在 Tornado 上运行为 Twisted 编写的代码。
Summary
概括
With Tornado you can use a code from Twisted. There is no need to use cyclone which only twistsyour code (your code becomes more messy).
使用 Tornado,您可以使用来自 Twisted 的代码。没有必要使用只会扭曲您的代码的旋风(您的代码变得更加混乱)。
As for 2014, Tornado is considered as widely accepted and default async framework which works both on python2 and python3. Also the latest version 4.x brings a lot of functionality from https://docs.python.org/dev/library/asyncio.html.
至于 2014 年,Tornado 被认为是被广泛接受的默认异步框架,它适用于 python2 和 python3。此外,最新版本 4.x 带来了许多来自https://docs.python.org/dev/library/asyncio.html的功能。
I wrote an article, explaining why I consider that Tornado - the best Python web frameworkwhere I wrote much more about Tornado functionality.
我写了一篇文章,解释了为什么我认为Tornado - 最好的 Python Web 框架,我在其中写了更多关于 Tornado 功能的文章。
回答by Erik Kaplun
(UPDATE: I'm sadly surprised about how few answers here recommend or even mention Gevent—I don't think it's in proportion to the popularity, performance and ease of use of this excellent library!)
(更新:我很遗憾这里推荐甚至提到 Gevent 的答案很少——我认为这与这个优秀库的受欢迎程度、性能和易用性不成比例!)
Gevent and Twisted are not mutually exclusive, even though the contrary might seem obvious at first. There is a project called geventreactorwhich allows one to relatively smoothly leverage the best of both worlds, namely:
Gevent 和 Twisted 并不相互排斥,尽管乍一看似乎相反。有一个名为的项目geventreactor,它允许人们相对顺利地利用两全其美,即:
- The efficient and cheap (cooperative green) thread model of Gevent, which is much easier to program in when it comes to concurrency—frankly, Twisted's
inlineCallbacksis simply not up to the job in terms of performance when it comes to many coroutines, and neither in terms of ease/transparency of use:yieldandDeferredseverywhere; often hard to build some abstractions; horrifyingly useless stack traces with both bareDeferreds as well as, and even more so with@inlineCallbacks. - All the built-in functionality of Twisted you can ever dream of, including but not limited to
IReactorProcess.spawnProcess.
- Gevent 的高效廉价(合作绿色)线程模型,在并发方面更容易编程 - 坦率地说,
inlineCallbacks当涉及到许多协程时,Twisted 的性能根本无法胜任,而且在易用性/透明性条款:yield以及Deferreds任何地方;通常很难建立一些抽象;使用裸Deferreds 和@inlineCallbacks. - 您梦寐以求的 Twisted 的所有内置功能,包括但不限于
IReactorProcess.spawnProcess.
I'm personally currently using Gevent 1.0rc2 with Twisted 12.3 bridged by geventreactor. I have implemented my own as-of-yet unpublished additions and enhancements to geventreactorwhich I will publish soon, hopefully as part of geventreactor's original GitHub repository: https://github.com/jyio/geventreactor.
我个人目前使用 Gevent 1.0rc2 和 Twisted 12.3 桥接geventreactor。我已经实现了我自己的尚未发布的添加和改进geventreactor,我将很快发布,希望作为geventreactor原始 GitHub 存储库的一部分:https: //github.com/jyio/geventreactor。
My current layout allows me to program in the nice programming model of Gevent, and leverage things such as a non-blocking socket, urllib2and other modules. I can use regular Python code for doing regular things, as opposed to the learning curve and inconvenience of doing even simple, basic things the Twisted way. I can also easily use most 3rd party libraries that are normally either out of question with Twisted, or require the use of threads.
我目前的布局可以让我在节目中GEVENT漂亮的编程模型,并利用的东西,如非阻塞socket,urllib2和其他模块。我可以使用常规的 Python 代码来做常规的事情,而不是用 Twisted 方式做简单的基本事情的学习曲线和不便。我还可以轻松使用大多数 3rd 方库,这些库通常在 Twisted 中没有问题,或者需要使用线程。
I can also completely avoid the awkward and often overly complex callback based programming by using greenlets (instead of Deferreds and callbacks, and/or @inlineCallbacks).
我还可以通过使用 greenlets(而不是Deferreds 和回调和/或@inlineCallbacks)来完全避免笨拙且通常过于复杂的基于回调的编程。
(This answer was written based on my personal experiences having used both Twisted and Gevent in real life projects, with significantly more experience using Twisted (but I don't claim to be a Twisted expert). The software I've had to write hasn't had to use too many of Twisted's features, so depending on the set of features you require of Twisted, the (relatively painless) extra complexity of mixing Gevent and Twisted might not be worth the trouble.)
(这个答案是根据我在现实生活项目中使用 Twisted 和 Gevent 的个人经验编写的,使用 Twisted 的经验明显更多(但我并不声称自己是 Twisted 专家)。我必须编写的软件没有不必使用太多 Twisted 的功能,因此根据您需要的 Twisted 功能集,混合 Gevent 和 Twisted 的(相对轻松)额外的复杂性可能不值得麻烦。)

