Real time server

2,649 views

Published on

Real time web serving with Tornado

Published in: Technology

Real time server

  1. 1. Real-time Webserving <ul><li>Playing around with Tornado Web Server </li></ul>
  2. 2. Lean Basics <ul><li>While Tornado is similar to existing Web-frameworks in Python (Django, Google's webapp, web.py ), it focuses on speed and handling large amounts of simultaneous traffic. </li></ul>
  3. 3. <ul><li>RSS reader flagged/read/favourite/selected </li></ul><ul><li>Show current status/price for a number of products </li></ul><ul><li>Ask the server to process an image </li></ul><ul><li>Routing chat between webapps </li></ul>Web User Scenarios
  4. 4. Server Startup Multi sub-domain, auto-reloading process Multi sub-domain, auto-reloading process ioloop = tornado.ioloop.IOLoop.instance() for n in structure.SITES: site = structure.SITES[n] if site[&quot;package&quot;] in (&quot;tornado&quot;, &quot;mediaserver&quot;): server = HTTPServer( Application (urls,site,ioloop=ioloop)) server.listen(site[&quot;port&quot;]) tornado.autoreload.start(io_loop=ioloop) ioloop.start()
  5. 5. Handle Request urls = [ (r&quot;/static/(.*)&quot;, StaticFileHandler),] class MyHandler(tornado. web.RequestHandler ): def get(self,subpath): info = { “ abc_list”: [“a”, ”b”, ”c”], “ path”: self.request.path, “ subpath”: subpath } self.render(&quot;home.html&quot;,**info) def post(self,subpath): post_names = self.request.arguments.keys() headers_dict = self.request.headers headers_dict = self.request.headers
  6. 6. Serve Response class MyHandler(tornado. web.RequestHandler ): def get(self,subpath): site_title = self.application.site[&quot;title&quot;] info = { “ abc_list”: [“a”, ”b”, ”c”], “ site_title”: site_title } self.render(&quot;home.html&quot;,**info) def post(self): post_names = self.request.arguments.keys(): self.set_header(&quot;Content-Type&quot;,&quot;text/plain&quot;) self.write('done.')
  7. 7. Delayed Response class MainHandler(tornado. web.RequestHandler ): @ tornado. web.asynchronous def get(self): http = tornado.httpclient.AsyncHTTPClient() http.fetch(&quot; http://friendfeed-api.com/v2/feed/ bret &quot;, callback=self .async_callback( self.on_response )) def on_response(self, response): if response.error: raise tornado. web.HTTPError (500) json = tornado.escape.json_decode(response.bod y) sel f.write(&quot;Fetched &quot; + str(len(json[&quot;entries&quot;])) + &quot; entries &quot; &quot;from the FriendFeed API&quot;) self.finish ()
  8. 8. Baseline Performance <ul><li>Using Apache Bench </li></ul>
  9. 9. Henrik Vendelbo <ul><li>[email_address] </li></ul><ul><li>http://github.com/thepian </li></ul>
  10. 10. Baseline Performance <ul><li>Using Apache Bench </li></ul>

×