Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Adapting blocking calls to Tornado coroutines with run_on_executor decorators
import random
import time
from tornado import gen
from tornado.concurrent import run_on_executor, futures
from tornado.ioloop import IOLoop
class TaskRunner(object):
def __init__(self, loop=None):
self.executor = futures.ThreadPoolExecutor(4)
self.loop = loop or IOLoop.instance()
def long_running_task(self):
tau = random.randint(0, 3)
return tau
loop = IOLoop() # this is necessary if running as an ipynb!
tasks = TaskRunner(loop)
def do_stuff():
result = yield tasks.long_running_task()
raise gen.Return(result)
def do_other_stuff():
def main():
for i in range(10):
stuff = yield do_stuff()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.