import time
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor
MAX_WORKERS = 4
class Handler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
@run_on_executor
def background_task(self, i): """ This will be executed in `executor` pool. """
time.sleep(10)
return i
@tornado.gen.coroutine
def get(self, idx): """ Request that asynchronously calls background task. """
res = yield self.background_task(idx)
self.write(res)