Created
May 5, 2014 19:47
-
-
Save Lothiraldan/f359766d1c750f6b6cb2 to your computer and use it in GitHub Desktop.
Tornado + Asyncio
This file contains hidden or 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
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