chris
chris

Reputation: 7593

combine asyncio and tornado

Given that in version 5 of Tornado tornado.ioloop.IOLoop.current() is asyncio event loop when available - how does one go about ensuring that an aiohttp web-scraping script called from a handler uses the same event loop?

Are there any examples around of such a setup?

Thanks

Upvotes: 1

Views: 623

Answers (1)

Ben Darnell
Ben Darnell

Reputation: 22154

It should just work by default. Here's a simple example:

from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, Application
import aiohttp

class MyHandler(RequestHandler):
    async def get(self):
        async with aiohttp.ClientSession() as session:
            async with session.get("https://www.google.com/robots.txt") as resp:
                self.write(await resp.text())

app = Application([('/', MyHandler)])
app.listen(8080)
IOLoop.current().start()

Upvotes: 2

Related Questions