Real time server

10
Real-time Webserving Real-time Webserving Playing around with Tornado Web Playing around with Tornado Web Server Server

description

Real time web serving with Tornado

Transcript of Real time server

Page 1: Real time server

Real-time WebservingReal-time WebservingPlaying around with Tornado Web ServerPlaying around with Tornado Web Server

Page 2: Real time server

Lean Lean BasicsBasicsWhile Tornado is similar to While Tornado is similar to existing Web-frameworks in existing Web-frameworks in Python (Django, Google's Python (Django, Google's webapp, webapp, web.pyweb.py), it focuses ), it focuses on speed and handling large on speed and handling large amounts of simultaneous amounts of simultaneous traffic.traffic.

Page 3: Real time server

RSS reader RSS reader flagged/read/favourite/selectedflagged/read/favourite/selected

Show Show currentcurrent status/price for a number status/price for a number of productsof products

Ask the server to process an imageAsk the server to process an image

Routing chat between webappsRouting chat between webapps

Web User ScenariosWeb User Scenarios

Page 4: Real time server

Server StartupServer StartupMulti sub-domain, auto-reloading processMulti sub-domain, auto-reloading process

ioloop = tornado.ioloop.IOLoop.instance() for n in ioloop = tornado.ioloop.IOLoop.instance() for n in structure.SITES: site = structure.SITES[n] if structure.SITES: site = structure.SITES[n] if site["package"] in ("tornado", "mediaserver"): server = site["package"] in ("tornado", "mediaserver"): server = HTTPServer(HTTPServer(ApplicationApplication(urls,site,ioloop=ioloop)) (urls,site,ioloop=ioloop)) server.listen(site["port"]) server.listen(site["port"]) tornado.autoreload.start(io_loop=ioloop) ioloop.start()tornado.autoreload.start(io_loop=ioloop) ioloop.start()

Page 5: Real time server

Handle RequestHandle Request

urls = [urls = [ (r"/static/(.*)", StaticFileHandler),](r"/static/(.*)", StaticFileHandler),]

class MyHandler(tornado.class MyHandler(tornado.web.RequestHandler): def ): def get(self,subpath):get(self,subpath): info = {info = { “ “abc_list”: [“a”, ”b”, ”c”],abc_list”: [“a”, ”b”, ”c”], “ “path”: self.request.path,path”: self.request.path, “ “subpath”: subpathsubpath”: subpath } self.render("home.html",**info) def } self.render("home.html",**info) def post(self,subpath): post_names = post(self,subpath): post_names = self.request.arguments.keys()self.request.arguments.keys() headers_dict = self.request.headersheaders_dict = self.request.headers

Page 6: Real time server

Serve ResponseServe Response

class MyHandler(tornado.class MyHandler(tornado.web.RequestHandler): def ): def get(self,subpath):get(self,subpath): site_title = self.application.site["title"] info = {site_title = self.application.site["title"] info = { “ “abc_list”: [“a”, ”b”, ”c”],abc_list”: [“a”, ”b”, ”c”], “ “site_title”: site_titlesite_title”: site_title } self.render("home.html",**info)} self.render("home.html",**info) def post(self): post_names = def post(self): post_names = self.request.arguments.keys(): self.set_header("Content-self.request.arguments.keys(): self.set_header("Content-Type","text/plain") self.write('done.')Type","text/plain") self.write('done.')

Page 7: Real time server

Delayed ResponseDelayed Response

class MainHandler(tornado.class MainHandler(tornado.web.RequestHandler): ): @@tornado.tornado.web.asynchronousweb.asynchronous def get(self): http = tornado.httpclient.AsyncHTTPClient() http.fetch("http://friendfeed-api.com/v2/feed/http://friendfeed-api.com/v2/feed/bret", callback=self.async_callback(self.on_responseself.on_response)) )) def on_response(self, response): if response.error: raise def on_response(self, response): if response.error: raise tornado.tornado.web.HTTPErrorweb.HTTPError(500) json = (500) json = tornado.escape.json_decode(response.bodtornado.escape.json_decode(response.body) self.write("Fetched " + str(len(json["entries"])) + " entries " "from the FriendFeed API") self.finishself.finish()()

Page 8: Real time server

Baseline PerformanceBaseline PerformanceUsing Apache BenchUsing Apache Bench

Page 9: Real time server

Henrik VendelboHenrik [email protected]@googlemail.com

http://github.com/thepianhttp://github.com/thepian

Page 10: Real time server

Baseline PerformanceBaseline PerformanceUsing Apache BenchUsing Apache Bench