Skip to content

Instantly share code, notes, and snippets.

@csytan
Created April 18, 2014 21:19
Show Gist options
  • Save csytan/11064907 to your computer and use it in GitHub Desktop.
Save csytan/11064907 to your computer and use it in GitHub Desktop.
Tornado async handler with threading
import time
# For python 2.7 use:
# pip install futures
import concurrent.futures
import tornado.ioloop
import tornado.web
import tornado.gen
thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=4)
def future_thread(func):
def wrapper(*args, **kwargs):
return thread_pool.submit(func, *args, **kwargs)
return wrapper
@future_thread
def blocking():
time.sleep(10)
class Index(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self, block=False):
if block:
yield blocking()
self.write('hello world')
app = tornado.web.Application([
(r'/', Index),
(r'/(block)', Index)
])
app.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment