Created
November 2, 2015 02:12
-
-
Save vuuvv/29bc5600ac9fa1fad7e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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