Skip to content

Instantly share code, notes, and snippets.

@randy-ran
Forked from methane/gist:2185380
Last active August 29, 2015 14:12
Show Gist options
  • Save randy-ran/11c130671633285f6d47 to your computer and use it in GitHub Desktop.
Save randy-ran/11c130671633285f6d47 to your computer and use it in GitHub Desktop.
from time import sleep
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application, asynchronous, RequestHandler
from multiprocessing.pool import ThreadPool
_workers = ThreadPool(10)
def run_background(func, callback, args=(), kwds={}):
def _callback(result):
IOLoop.instance().add_callback(lambda: callback(result))
_workers.apply_async(func, args, kwds, _callback)
# blocking task like querying to MySQL
def blocking_task(n):
sleep(n)
return n
class Handler(RequestHandler):
@asynchronous
def get(self):
run_background(blocking_task, self.on_complete, (10,))
def on_complete(self, res):
self.write("Test {0}<br/>".format(res))
self.finish()
HTTPServer(Application([("/", Handler)],debug=True)).listen(8888)
IOLoop.instance().start()
@randy-ran
Copy link
Author

Comment for lone strangers (as i was here):

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)

Docs: http://tornado.readthedocs.org/en/stable/concurrent.html#tornado.concurrent.run_on_executor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment