Search code examples
tornado

Tornado asyncio questions


Currently all my functions are decorated with @tornado.gen.coroutine which i then yield. I want to move to the newer async/await.

To move to async does that mean that i need to start the Tornado with the asyncio IOLoop as shown in documentation:

from tornado.platform.asyncio import AsyncIOMainLoop
import asyncio
AsyncIOMainLoop().install()
asyncio.get_event_loop().run_forever()

I changed some co-routines to use async/await instead of yield this seemed to work on the old Tornado IOLoop why is that if i didn't start the Tornado engine explicitly with asyncio?

In my current code I have a place where I yield a list of coroutines:

responses=yield [wrap(httpClient,url) for url in somelist]

how could this line be achieved with await?


Solution

  • You do not need to use asyncio's event loop, Tornado can run native coroutines (defined with async def) using the Tornado event loop.

    You can await a list of coroutines like:

    await tornado.gen.multi([wrap(httpClient,url) for url in somelist])
    

    More information about Tornado and native coroutines is in the docs.