Your SlideShare is downloading. ×
0
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Tornado - different Web programming
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Tornado - different Web programming

1,253

Published on

Slides from talk about Tornado Web framework for Dnipro Ruby community

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,253
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
31
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. –different Web programming Dima Malenko for Ruby Meetup
  • 2. C++ Win32 MFC CORBA2002
  • 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. Web Programming Model Request with parametersClient Server HTML response
  • 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. 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. Why the “separate thread”?
  • 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. 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. C10kproblemhttp://www.kegel.com/c10k.html
  • 11. Non-blocking I/O!
  • 12. What would you put in front of loaded Web site?
  • 13. Why Nginx?
  • 14. Ability to handle more than 10,000simultaneous connections with alow memory footprint (~2.5 MBper 10k inactive HTTP keep-aliveconnections) Wikipedia
  • 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. • Python web framework and async networking library• Originally developed for FriendFeed• Ideal for long polling, WebSockets and long- lived connections www.tornadoweb.com
  • 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. 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. 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. And That’s It?
  • 21. Web Site Node Show list of currently Runs many real-timerunning apps with their applications titles App Changes titles whenever it wants
  • 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. 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. 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. 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. How is it possible?Cooperative Multitasking
  • 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. 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. 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. Questions? www.dmalenko.org dmalenko@rollapp.com @dmalenko

×