Tornado Server Experiments
Revisionas of Saturday, 22 May 2021 at 17:53 UTC
importtornado.httpserverimporttornado.ioloopimporttornado.concurrentfromtornado.optionsimport(define,options,parse_command_line)importtornado.webclassSyncHandler(tornado.web.RequestHandler):"""Simple request handler that blocks the IOLoop
"""defget(self,seconds):blocking_task(int(seconds))self.write("Blocked {}".format(seconds))classAsyncHandler(tornado.web.RequestHandler):"""Use Tornado's coroutine generators to handle an incoming request.
A slow, blocking function is submitted to an execution pool and
control is released back to the Tornado server's IOLoop to
service other incoming requests.
"""# Create a thread poolimportconcurrentexecutor=concurrent.futures.ThreadPoolExecutor(10)@tornado.concurrent.run_on_executordefblocking_task(self,n=10):fromtimeimportsleepprint("Sleeping {}".format(n))sleep(n)returnn@tornado.gen.coroutinedefget(self,seconds):yieldself.blocking_task(int(seconds))self.write("Blocked {}".format(seconds))classIndexHandler(tornado.web.RequestHandler):"""Handle the index page
"""defget(self):self.write("I'm a Tornado server")define("port",default=8000,help="Port to run Tornado instance on")define("reload",default=False,help="Reload server on change")define("debug",default=False,help="Show debug logs")if__name__=='__main__':parse_command_line()tornado.httpserver.HTTPServer(tornado.web.Application(handlers=[(r"/",IndexHandler),(r"/sync\/?(\d+)",SyncHandler),(r"/async\/?(\d+)",AsyncHandler)],autoreload=options.reload,debug=options.debug,)).listen(options.port)tornado.ioloop.IOLoop.instance().start()
importtornado.httpserverimporttornado.ioloopfromtornado.optionsimport(define,options,parse_command_line)importtornado.web# Create a thread poolimportconcurrentexecutor=concurrent.futures.ThreadPoolExecutor(10)defblocking_task(n=10):fromtimeimportsleepprint("Sleeping {}".format(n))sleep(n)classSyncHandler(tornado.web.RequestHandler):"""Simple request handler that blocks the IOLoop
"""defget(self,seconds):blocking_task(int(seconds))self.write("Blocked {}".format(seconds))classAsyncHandler(tornado.web.RequestHandler):"""Use Tornado's coroutine generators to handle an incoming request.
A slow, blocking function is submitted to an execution pool and
control is released back to the Tornado server's IOLoop to
service other incoming requests.
There's also a @run_on_executor decorator that can be applied
to the blocking function to prettify things.
E.g. https://gist.github.com/methane/2185380#comment-1301483
References
----------
* http://tornado.readthedocs.org/en/latest/guide/coroutines.html
* http://blog.trukhanov.net/Running-synchronous-code-on-tornado-asynchronously/
* http://lbolla.info/blog/2013/01/22/blocking-tornado
"""@tornado.gen.coroutinedefget(self,seconds):yieldexecutor.submit(blocking_task,int(seconds))self.write("Blocked {}".format(seconds))classIndexHandler(tornado.web.RequestHandler):"""Handle the index page
"""defget(self):self.write("I'm a Tornado server")define("port",default=8000,help="Port to run Tornado instance on")define("reload",default=False,help="Reload server on change")define("debug",default=False,help="Show debug logs")if__name__=='__main__':parse_command_line()tornado.httpserver.HTTPServer(tornado.web.Application(handlers=[(r"/",IndexHandler),(r"/sync\/?(\d+)",SyncHandler),(r"/async\/?(\d+)",AsyncHandler)],autoreload=options.reload,debug=options.debug,)).listen(options.port)tornado.ioloop.IOLoop.instance().start()