Skip to content

Instantly share code, notes, and snippets.

@xkou
Forked from arvidfm/asyncio-tornado.py
Created June 13, 2018 09:59
Show Gist options
  • Save xkou/ed130dd061c839d0ea79933f137df492 to your computer and use it in GitHub Desktop.
Save xkou/ed130dd061c839d0ea79933f137df492 to your computer and use it in GitHub Desktop.
Running Tornado on asyncio's event loop, including 'yield from' support in request handlers
import asyncio
import tornado.concurrent
import tornado.ioloop
import tornado.web
import tornado.platform.asyncio
import tornado.httpclient
class ReqHandler(tornado.web.RequestHandler):
async def get(self):
self.write("Hello world!\n")
print("Hej!")
await asyncio.sleep(2)
print("Hej igen!")
res = await tornado.httpclient.AsyncHTTPClient().fetch("http://google.com")
print(res)
self.write("Hello test\n")
app = tornado.web.Application([
(r'/', ReqHandler)
])
if __name__ == '__main__':
tornado.platform.asyncio.AsyncIOMainLoop().install()
app.listen(8080)
asyncio.get_event_loop().run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment