Introduction to Tornado - TienNA

2,270 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,270
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
23
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • 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
  • Introduction to Tornado - TienNA

    1. 1. Introduction to TornadoNguyen Anh Tien – 2013/06/08
    2. 2. ● Python web serverframework● FriendFeed– http://friendfeed.com/● Facebook, Bret Taylor● Scalable● Non-blocking I/OWhat is Tornado ?https://github.com/facebook/tornadohttp://www.tornadoweb.org/
    3. 3. Some services use Tornado
    4. 4. How to install● Lazy way– sudo apt-get install python-setuptools– sudo easy_install tornado
    5. 5. Famous “Hello, world” Exampleimport tornado.ioloopimport tornado.webclass 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. 6. Code breaking (1)application = tornado.web.Application([(r"/", MainHandler),])● Request to root path is handled by class MainHandlerclass MainHandler(tornado.web.RequestHandler):def get(self):self.write("Hello, world")● Implement GET function to handle GET request
    7. 7. Code breaking (2)if __name__ == "__main__":application.listen(8888)tornado.ioloop.IOLoop.instance().start()● Start server listen at port 8888● Simple, doesnt it ?
    8. 8. 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)
    9. 9. Features – Authentication● Signed secure cookie● Third party authentication– Facebook– Twitter– Google– OAuth
    10. 10. Features - Others● Localization● Cross-site request forgery protection● Static file● Caching● ...
    11. 11. Blocking I/O
    12. 12. Non blocking I/O
    13. 13. 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 andeasily
    14. 14. Tornado is not for● Complex CMS service, full MVC features→ Django is a good choice● Computational intensive tasks
    15. 15. Demo● Chat server– At 192.168.1.37:1234● Twitter recent post count
    16. 16. Reference● Introduction to Tornado –OReilly Media● http://www.tornadoweb.org● https://github.com/Introduction-to-Tornado● https://github.com/facebook/tornado● http://www.slideshare.net/kurtiss/tornado-web

    ×