Tornado - different Web programming

1,787 views

Published on

Slides from talk about Tornado Web framework for Dnipro Ruby community

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

No Downloads
Views
Total views
1,787
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
36
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Tornado - different Web programming

  1. 1. –different Web programming Dima Malenko for Ruby Meetup
  2. 2. C++ Win32 MFC CORBA2002
  3. 3. ASP.NET Web Formspublic class FirstPage: System.Web.UI.Page{ public System.Web.UI.WebControls.Label lblMessage; private int i = 0; protected void Page_Load(object sender, EventArgs e) { i = i + 1; lblMessage.Text = i.ToString(); }}
  4. 4. Web Programming Model Request with parametersClient Server HTML response
  5. 5. How Web Server Works?• Create socket• Bind socket• Listen on socket • Accept new connection from a client • Receive data from the client • Process the request somehow • Send response to the client• Wait for a new connection
  6. 6. How Web Server Works?• Create socket• Bind socket• Listen on socket • Accept new connection from a clientSeparate thread • Receive data from the client • Process the request somehow • Send response to the client• Wait for a new connection
  7. 7. Why the “separate thread”?
  8. 8. How Web Server Works?• Create socket• Bind socket• Listen on socket • Accept new connection from a clientSeparate thread • Receive data from the client • Process the request somehow • Send response to the client• Wait for a new connection
  9. 9. Threads cost• Memory overhead – Default thread stack size in Linux – 8M• Context switches cost – It takes ~2-3µs to switch context in Linux – http://blog.tsunanet.net/2010/11/how-long-does- it-take-to-make-context.html
  10. 10. C10kproblemhttp://www.kegel.com/c10k.html
  11. 11. Non-blocking I/O!
  12. 12. What would you put in front of loaded Web site?
  13. 13. Why Nginx?
  14. 14. Ability to handle more than 10,000simultaneous connections with alow memory footprint (~2.5 MBper 10k inactive HTTP keep-aliveconnections) Wikipedia
  15. 15. How is that possible?• Non-blocking networking using fastest interface on any given platform – kqueue (FreeBSD 4.1+) – epoll (Linux 2.6+) – rt signals (Linux 2.2.19+) – /dev/poll (Solaris 7 11/99+) – event ports (Solaris 10) – select – poll
  16. 16. • Python web framework and async networking library• Originally developed for FriendFeed• Ideal for long polling, WebSockets and long- lived connections www.tornadoweb.com
  17. 17. import 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()
  18. 18. Handling Requestsclass ProductHandler(tornado.web.RequestHandler): def get(self, product_id): self.write("Information about " + product_id) @tornado.web.authenticated def post(self, product_id): # store updated product infoapplication = tornado.web.Application([ (r"/product/([0-9]+)", ProductHandler)])
  19. 19. 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 = ["Item 1", "Item 2", "Item 3"] self.render("template.html", title="My title", items=items)
  20. 20. And That’s It?
  21. 21. Web Site Node Show list of currently Runs many real-timerunning apps with their applications titles App Changes titles whenever it wants
  22. 22. Web Site Node Show list of currently Runs many real-timerunning apps with their applications titlesHow would you make Appsure user can see updated Changes titlestitles on the web? whenever it wants
  23. 23. Web Site Node 300-400ms Show list of currently Runs many real-timerunning apps with their applications titlesWhat if… App App App App App App Changes titles Changes titles whenever it wants Changes titles whenever it wants Changes titles whenever it wants Changes titles whenever it wants Changes titles whenever it wants whenever it wants
  24. 24. Non-blocking Requestsclass AppUpdatesHandler(tornado.web.RequestHandler): @tornado.web.asynchronous def get(self): http = tornado.httpclient.AsyncHTTPClient() request = tornado.httpclient.HTTPRequest( "http://www.frontend.com", method="POST", ...) http.fetch(request, callback=self.on_response) self.finish() def on_response(self, response): if not response.error: log.debug("Title updated”)
  25. 25. WebSocketsclass WebSocket(websocket.WebSocketHandler): def open(self): print "WebSocket opened" def on_message(self, message): print "Message: " + message def on_close(self): print "WebSocket closed"
  26. 26. How is it possible?Cooperative Multitasking
  27. 27. How does it work?• Event loop – Tornado.ioloop.IOLoop – Level-triggered, uses epoll on Linux and kqueue on BSD• Futures – Result of an async operation
  28. 28. Coroutinesclass AppUpdatesHandler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.coroutine def get(self): http = tornado.httpclient.AsyncHTTPClient() request = tornado.httpclient.HTTPRequest( "http://www.frontend.com", method="POST", ...) self.finish() response = yield http.fetch(request) if not response.error: log.debug("Title updated”)
  29. 29. Conclusions• Web has changed, so programmers should too• You want to face C10k problem• There means to solve it• Learn to think asynchronously• Check out www.tornadoweb.org
  30. 30. Questions? www.dmalenko.org dmalenko@rollapp.com @dmalenko

×