gevent,eventlet,Twisted,Tornado各有什么区别和w7和w10的优劣区别

Evented Django part one: Socket.IO and gevent - 推酷
Evented Django part one: Socket.IO and gevent
Socket.IO was developed with a
server implementation, but work is being done to add server implementations to a variety of languages. Two such servers exist for Python,
. I'm a big fan of gevent, so I will use gevent-socketio, but tornadio looks well-written and very promising.
Why you should be thinking about gevent
Socket.IO runs great under Node.JS, but I think it's important to highlight why I think Python and gevent need more attention (feel free to skip ahead if you have already drank the gevent koolaid). Node.JS (and its underlying V8 Javascript engine) is a pinnacle achievement for the world of Javascript. It has done two especially important things: it helped show the world that evented application servers enable extremely fast high-concurrency connections, and it helped promote Javascript as a serious language, opening the doors for powerful tools such as
, and better community code standards. Its popularity is not surprising: it's built on top of one of the world's most well-known programming languages.
The Python community is a bit more fragmented, with several concurrent networking libraries -- among them: twisted, tornado, gevent, eventlet, and concurrence. It's certainly harder to know where to start without a &clear winner& like we see in the Javascript community. Personally, gevent has quickly become my favorite way to write asynchronous applications. I think Python with gevent wins over Node.JS in two important ways:
It's Python, a sane and concise language with an awesome standard library and community.
instead of callbacks to provide concurrency.
like Node.JS,
is built on libevent (
Update: Node actually uses libev. Thanks to Travis Cline for correcting me there
), an underlying C library that provides a high-speed event loop. Node's concurrency model relies on callbacks to handle values from asynchronous I/O calls. This, combined with Javascript's highly nestable syntax, begs programmers to nest functions within other function calls, making callback passing a piece of cake, but I've seen this produce ugly, unreadable nested code, and I've seen programmers pull their hair out while trying to get things synchronized and avoid race conditions. In my experience, debugging an app with heavy use of callbacks is nearly impossible. Greenlet changes the game, because you can write simple &blocking& code that only blocks the current greenlet instead of the entire interpreter, allowing you to maintain stacks, along with beautiful Python stack traces.
Running Django on gevent-socketio
Gevent-socketio comes with one important caveat: you must use the gevent pywsgi server. This means you can't serve your WSGI app out of Apache like you might be used to doing (however, it should be possible to proxy requests from a front-end load balancer, but I haven't experimented with proxying web sockets). There's a pretty good reason for this: WSGI doesn't inherently allow web sockets. The only way this is possible is by hooking into the raw socket using the hooks provided by the pywsgi server.
Gevent-socketio works by creating a SocketIO handler and adding it to the WSGI &environ& dictionary before executing your WSGI app. When Django handles a request, it creates a WSGIRequest object and assigns it the environ dictionary created by pywsgi. So, if we are running Django under gevent-socketio, our SocketIO handler is available by accessing &request.environ['socketio']&. I've demonstrated this by porting the
to Django. My ported code is
Installation
I always choose to work in virtualenv, and I've created a pip requirements file that should cover what you need to get started. To run my example, clone the code on Github and install the requirements from pip:
git clone git:///codysoyland/django-socketio-example.git
cd django-socketio-example
easy_install pip
pip install virtualenv
virtualenv .
source ./bin/activate
pip install -r pip_requirements.txt
Note the contents of pip_requirements.txt: I'm using the &tip& versions of both gevent-websocket and gevent-socketio. This is still beta-quality software, so we are using development versions.
Note: Expect bugs!
A chat server request handler
The Socket.IO calls come in like normal requests and can be handled by a view, but your view code can actually contain a long-running event loop, sending and receiving messages from your web client. Here is the view that handles Socket.IO requests:
from django.http import HttpResponse
buffer = []
def socketio(request):
socketio = request.environ['socketio']
if socketio.on_connect():
socketio.send({'buffer': buffer})
socketio.broadcast({'announcement': socketio.session.session_id + ' connected'})
while True:
message = socketio.recv()
if len(message) == 1:
message = message[0]
message = {'message': [socketio.session.session_id, message]}
buffer.append(message)
if len(buffer) & 15:
del buffer[0]
socketio.broadcast(message)
if not socketio.connected():
socketio.broadcast({'announcement': socketio.session.session_id + ' disconnected'})
return HttpResponse()
The view is plugged into your site like any other view:
urlpatterns += patterns('views',
(r'^socket\.io', 'socketio'),
Running the example
Run the example by starting the server:
./run_example.py
Then point your browser to http://localhost:9000/.
If you run the example, you should see the same result as running the gevent-socketio example: a multi-client chatroom. The beauty of greenlet is at play in the line containing &socketio.recv()&. This line blocks the greenlet and allows the server to keep processing other requests until a new Socket.IO message is ready to be processed. As soon as a new message is ready, the greenlet is re-awakened and the message is processed.
Note that we can't use our good old friend &./manage.py runserver& for this example. This is because we need to run the SocketIO server, which we import from gevent-socketio. Here is the example runner:
PORT = 9000
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
from socketio import SocketIOServer
if __name__ == '__main__':
print 'Listening on port %s and on port 843 (flash policy server)' % PORT
SocketIOServer(('', PORT), application, resource=&socket.io&).serve_forever()
This is all it takes to hook up gevent-socketio to the Django WSGIHandler. A monkey could easily make this into a custom management command if we desired.
Further reading
In my next post, I will explain how to scale our chatroom example to multiple web servers using ZeroMQ. Until then, I recommend checking out the following resources:
I would like to extend a special thanks to
and other contributors for writing gevent-websocket and gevent-socketio.
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致出处:http://simple-/
我在选一个python的互联网框架, 本来已经定下来用Tornado了.
但我还听到很多人推荐Flask的简单性和灵活性, 还有gevent的高性能, 所以决定也试试它们以及它们和Tornado的结合.
我的示例就比”Hello World”应用稍微复杂一点儿, 它用到了模板.
下面是代码:
1, 纯粹Flask (pure_flask.py)
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def main_handler():
return render_template('main_j2.html', messages=&whatever&,title=&home&)
if __name__ == '__main__':
app.run(port=8888, debug=False)
2, 纯粹Tornado (pure_tornado.py)
import os.path
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('main.html', page_title=&&, body_id=&&, messages=&whatever&,title=&home&)
settings = {
&static_path&:os.path.join(os.path.dirname(__file__),'static'),
&template_path&:&templates&,
application = tornado.web.Application([
(r&/&, MainHandler),
], **settings)
if __name__ == &__main__&:
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
3, Flask应用运行在Gevent上 (gevent_flask.py)
from gevent.wsgi import WSGIServer
from pure_flask import app
http_server = WSGIServer(('', 8888), app)
http_server.serve_forever()
4, Flask应用运行在Tornado上 (tornado_flask.py)
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from pure_flask import app
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(8888)
IOLoop.instance().start()
5, Tornado应用运行在Gevent上 (gevent_tornado.py)
import tornado.wsgi
import gevent.wsgi
import pure_tornado
application = tornado.wsgi.WSGIApplication([
(r&/&, pure_tornado.MainHandler),
],**pure_tornado.settings)
if __name__ == &__main__&:
server = gevent.wsgi.WSGIServer(('', 8888), application)
server.serve_forever()
程序用到了三个模板文件: main.html, layout.html, and form.html. 主程序只调用了main.html.
main.html扩展(“extends”)了 layout.html, 并包含(“include”)了 form.html. 三个模板文件加起来有30kB大小.
Flask和Tornado用了不同的模板文件(Tornado是main.html, Flask是main_j2.html), 这是因为Flask和Tornado的模板语法虽然相似, 但有些地方不一样.
比如Flask(用Jinja2)模板的”block”用”{% endblock %}”表示结束, “for”用”{% endfor %}”; 但是Tornado模板则只用”{% end %}”.
我用ApacheBench测了这五个的requests per second:
ab -n 1000 -c 4 http://localhost:8888/
每个测五次. 我的机型是6年老的双核Opteron 254服务器.
下面是结果:
pure_flask: 82 88 107 102 71
pure_tornado: 144 244 241 294 290
gevent_flask: 127 139 145 152 110
tornado_flask: 110 88 74 92 101
gevent_tornado: 328 555 177 273 153
pure_flask: 90
pure_tornado: 242
gevent_flask: 135
tornado_flask: 93
gevent_tornado: 297
从上面结果可能看出Tornado比Flask快很多. 而Gevent可以让Tornado更快一些, 但快不了很多.
最终我还是喜欢Tornado应用的简单明了, 用Flask写稍微大一点的应用比较繁琐(比如要使用蓝图-blueprints等), 所以还是决定使用Tornado了.
在微博上关注:
- 创业公司
- 赛诺(武汉)技术有限公司
- 赛诺(武汉)技术有限公司
- 北京尚学硕博教育咨询
- 新浪云计算
相关 [flask tornado gevent] 推荐:
- Ken - (jobs, news)
英文: /2011/10/performance-of-flask-tornado-gevent-and.html. 我在选一个python的互联网框架, 本来已经定下来用Tornado了.
但我还听到很多人推荐Flask的简单性和灵活性, 还有gevent的高性能, 所以决定也试试它们以及它们和Tornado的结合.
- yinseny - (jobs, news)
本文有一个格式好看一点,并且有语法高亮的版本放在 readthedocs,欢迎浏览. 本文是原创,不是翻译,不过本文其实是谈翻译的. 话说用 wordpress 的 WYSIWYG 编辑器写这样的文章真痛苦啊,格式一不小心就乱了,本文是用 rst 写成,编译为 html,然后贴到这边来的. 最近用 Flask 给公司做了个小 web 应用,做的时候用英文了,现在要求翻译成中文.
- 博客园_首页
最近很多朋友都在问我关于 Flask 部署的问题,说实在的我很乐意看到和回答这样的问题,至少证明了越来越多人开始用 Flask 了. 之前我曾发表过一篇在 Ubuntu 上用 uwsgi + nginx 的 Flask 部署方法,说实在的 uwsgi 是个大坑可能用在 Django 上还好吧,不过用在 Flask 上未必就如此.
现在流行的静态博客/网站生成工具有很多,比如 Jekyll, Pelican, Middleman, Hyde 等等,
StaticGen 列出了目前最流行的一些静态网站生成工具. 我们的内部工具由 Python/Flask/MongoDB 搭建,现在需要加上文档功能,写作格式是 Markdown,不想把文档放到数据库里,也不想再弄一套静态博客工具来管理文档,于是找到了
Flask-FlatPages 这个好用的 Flask 模块.
- sasiky - (jobs, news)
内容索引 Table of Contents. 4.1
请求处理和请求参数. 4.2
RequestHandler中的主要方法. 4.4
Cookies和安全Cookies. 4.6
跨站伪造请求的防范. 4.7
静态文件和主动式文件缓存. 4.10
非阻塞式的异步请求. 7
WSGI 和 Google AppEngine.
- Ken - (jobs, news)
Tornado的核心源码是由ioloop.py和iostream.py这2个文件组成的. 前者提供了一个循环,用于处理I/O事件;后者则封装了一个非阻塞的socket. 有了这2者后,就能搭建起TCP server和HTTP server,实现异步HTTP客户端,这便是Tornado的主要内容了. 之前在研究socket时已差不多弄懂了ioloop的逻辑,于是本文就接着研究iostream了.
- LinuxTOY
Torrent Tornado 是一款完全使用 JavaScript 实现的附加组件,可以为 Firefox 浏览器增加 BT 下载功能. 体积小巧(不到 100K),完全使用 JavaScript 实现,跨平台且无本地二进制依赖. 支持和磁力链接及种子文件关联. 注意 当前 1.0 版本仅支持下载,不支持上传.
- ITeye博客
关于tornado,我这里就不详细讲了,有兴趣的同学可以通过以下两篇博客了解一下:. 我们一般用tornado来编写web程序,但实际上,tornado底层的代码非常优秀,也可以用这些代码来编写TCP应用. tornado最突出的特点就是“异步”,所以,我这里编写了一个异步的TCPServer和一个异步的TCPClient来帮助大家理解,下面直接看代码:.
- frank28_nfls - 阿福的技术BLOG
利用协程,在一个函数中从头写到尾就能实现并行,实在是太爽了. 相比之下,twisted中眼花缭乱的回调,学习曲线实在是太陡峭了.
- jin - (jobs, news)
我不想用很多时间去描述Gevent是什么,我想它官网上的一句总结足矣:. “Gevent是一种基于协程的Python网络库,它用到Greenlet提供的,封装了libevent事件循环的高层同步API. 接下来我将阐述在Mixpanel中一个内部项目使用Gevent的经验. 为了这篇文章我还动手写了几个性能小测试.
坚持分享优质有趣的原创文章,并保留作者信息和版权声明,任何问题请联系:@。gevent、eventlet、Twisted、Tornado各有什么区别和优劣? - 生活_【北京联盟】
gevent、eventlet、Twisted、Tornado各有什么区别和优劣?
/ 作者:admin
北京联盟摘要:
gevent、eventlet、Twisted、Tornado各有什么区别和优劣?,上一篇:
下一篇: 。另外Node.js如何? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 网友回复:
另外Node.js如何?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~网友回复:
gevent、eventlet、Twisted、Tornado各有什么区别和优劣?
免责声明:本站文章除注明来源“北京联盟”外的文章均来自网络和网友投稿,著作权归原作者所有。北京联盟不承担任何连带责任!

我要回帖

更多关于 w7和w10的优劣区别 的文章

 

随机推荐