Tornado is a Python web framework and asynchronous networking library. It is optimized for non-blocking I/O and can handle a large number of open connections efficiently. Tornado supports features like templates, authentication, and static files. It is well-suited for real-time applications like chat servers that require long-polling connections. Examples of Tornado applications include chat servers and services that count recent Twitter posts.
2. ● Python web server
framework
● FriendFeed
– http://friendfeed.com/
● Facebook, Bret Taylor
● Scalable
● Non-blocking I/O
What is Tornado ?
https://github.com/facebook/tornado
http://www.tornadoweb.org/
4. How to install
● Lazy way
– sudo apt-get install python-setuptools
– sudo easy_install tornado
5. Famous “Hello, world” Example
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
6. Code breaking (1)
application = tornado.web.Application([
(r"/", MainHandler),
])
● Request to root path is handled by class MainHandler
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
● Implement GET function to handle GET request
7. Code breaking (2)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
● Start server listen at port 8888
● Simple, doesn't it ?
8.
9. Features - Templates
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<ul>
{% for item in items %}
<li>{{ escape(item) }}</li>
{% end %}
</ul>
</body>
</html>
class MainHandler(tornado.web.RequestHandler):
def get(self):
items = ["First item", "Second", "Last One"]
self.render(
"template.html",
title="My title",
items=items
)
10. Features – Authentication
● Signed secure cookie
● Third party authentication
– Facebook
– Twitter
– Google
– OAuth
14. Tornado is ideal for
● Long polling service
– Keep-alive connection, real-time
● A lot of user
→ Chat Server, Notification Sever
● I/O intensive tasks
● Write super fast web applications quickly and
easily
15. Tornado is not for
● Complex CMS service, full MVC features
→ Django is a good choice
● Computational intensive tasks
17. Reference
● Introduction to Tornado –
O'Reilly Media
● http://www.tornadoweb.org
● https://github.com/Introduction-to-Tornado
● https://github.com/facebook/tornado
● http://www.slideshare.net/kurtiss/tornado-web
Notas do Editor
Handling many thousands concurrent requests is usually costly because the server needs to spawn threads to handle these and threads aren't that cheap The libraries you use have to be thread safe, meaning that multiple threads should be able to use them concurrently
Handling many thousands of concurrent requests isn't a problem and non-blocking servers excel at this and the reason why they are used for Comet Everything performed in the IO loop has to be non-blocking, else the IO loop gets blocked and your server will stall until this blocking operation is complete Thread safety isn't an issue