Skip to content

Instantly share code, notes, and snippets.

@vuuvv
Created November 2, 2015 02:12
Show Gist options
  • Save vuuvv/29bc5600ac9fa1fad7e8 to your computer and use it in GitHub Desktop.
Save vuuvv/29bc5600ac9fa1fad7e8 to your computer and use it in GitHub Desktop.
import time
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor # `pip install futures` for python2
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment