Skip to content

Instantly share code, notes, and snippets.

@arvidfm
Last active December 4, 2018 12:56
Show Gist options
  • Save arvidfm/11067131 to your computer and use it in GitHub Desktop.
Save arvidfm/11067131 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()
@zapu
Copy link

zapu commented Aug 28, 2016

Woah, I keep bumping into this gist from google results every now and then, thanks for keeping it updated!

@kzahel
Copy link

kzahel commented Sep 23, 2016

Can you show how to use PeriodicCallback as well? Or should I just use asyncio.Task instead to do periodic work?

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