Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Created March 24, 2015 01:16
Show Gist options
  • Save alejandrobernardis/80518a70a40e3c388f08 to your computer and use it in GitHub Desktop.
Save alejandrobernardis/80518a70a40e3c388f08 to your computer and use it in GitHub Desktop.
Pepo!
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