Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tornado + Asyncio
from tornado.platform.asyncio import AsyncIOMainLoop
import asyncio
import tornado
import tornado.web
import tornado.gen
from inspect import isgeneratorfunction, isgenerator
AsyncIOMainLoop().install()
loop = asyncio.get_event_loop()
def test_asyncio():
future = tornado.gen.Future()
@asyncio.coroutine
def set_result():
yield from asyncio.sleep(1)
future.set_result(42)
asyncio.Task(set_result())
return future
class MainHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
result = yield test_asyncio()
self.write('Result is {0}'.format(result))
class SecondHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
self.write('Result is 43')
application = tornado.web.Application([
(r"/", MainHandler),
(r"/sync", SecondHandler)
])
if __name__ == '__main__':
application.listen(8888)
try:
loop.run_forever()
finally:
loop.close()
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.