Skip to content

Instantly share code, notes, and snippets.

@somma
Last active August 29, 2015 14:16
Show Gist options
  • Save somma/eeef47388a9db54a23f0 to your computer and use it in GitHub Desktop.
Save somma/eeef47388a9db54a23f0 to your computer and use it in GitHub Desktop.
This is sample code about how to handle synchronous function as asynchronously in `tornado.web.RequestHandler` using `@tornado.gen.coroutine`.
#!/usr/bin/python
# -*- coding:utf-8 -*-
"""
This is sample code about how to handle synchronous function
as asynchronously in `tornado.web.RequestHandler` using `@tornado.gen.coroutine`.
Feel free to suggest idea or nicer python style code.
I'm a newbie to python :-)
by somma (fixbrain@gmail.com)
"""
import time
import threading
import tornado.gen
import tornado.web
import tornado.httpclient
import concurrent.futures
thread_pool = concurrent.futures.ThreadPoolExecutor(4)
def very_slow_function(arg1):
time.sleep(20)
print 'very_slow_function() returned....'
return arg1
def very_slow_function_async(arg1):
return thread_pool.submit(very_slow_function, arg1)
def warp_async_and_return_future(func):
"""
decorator wraps synchornous function and return future.
"""
def ret_future(*args, **kwargs):
return thread_pool.submit(func, *args, **kwargs)
return ret_future
@warp_async_and_return_future
def very_slow_function_decorated(arg1):
time.sleep(20)
print 'very_slow_function_decorated() returned....'
return arg1
class RootHandlerAsync(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
result = yield very_slow_function_async(100)
self.write("very_slow_function_async(), ret_value = {0}".format(result))
class RootHandlerAsync2(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
result = yield very_slow_function_decorated(100)
self.write("very_slow_function_decorated(), ret_value = {0}".format(result))
class GetFullPageAsyncHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
http_client = tornado.httpclient.AsyncHTTPClient()
http_response = yield http_client.fetch("http://www.drdobbs.com/web-development")
response = http_response.body.decode().replace(
"Most Recent Premium Content", "Most Recent Content")
self.write(response)
self.set_header("Content-Type", "text/html")
class FetchHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self, url_to_fetch):
http_client = tornado.httpclient.AsyncHTTPClient()
http_response = yield http_client.fetch(url_to_fetch)
self.write(http_response.body)
self.set_header("Content-Type", "text/html")
if __name__ == "__main__":
application = tornado.web.Application([
tornado.web.url(r"/", RootHandlerAsync),
tornado.web.url(r"/2", RootHandlerAsync2),
tornado.web.url(r"/async", GetFullPageAsyncHandler),
tornado.web.url(r"/fetch/(.+)", FetchHandler),
])
application.listen(8080)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment