Tornado: asyncio.wait_for not working

Created on 14 Feb 2017  路  5Comments  路  Source: tornadoweb/tornado

Hi, I know there are some incompatibilities with asyncio and I'm writing here to double check. I have the following code that is hanging indefinitely in python3.5.2 and also in python3.6.0b3:

import asyncio
import tornado.web
import tornado.ioloop
from datetime import datetime

class MainHandler(tornado.web.RequestHandler):

    async def time(self):
        return datetime.now().isoformat()

    async def get(self):
        await asyncio.wait_for(self.time(), 5)     # Here I'll hang
        self.write(await self.time())


if __name__ == "__main__":
    tornado.ioloop.IOLoop.configure('tornado.platform.asyncio.AsyncIOLoop')
    app = tornado.web.Application([(r"/", MainHandler)])
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

is this expected behavior? I'm maintaining https://github.com/argaen/aiocache and would like to support asyncio Tornado too.

Thanks

Most helpful comment

I think this is a bit different from #1543. That issue is about the differences between the tornado and asyncio event loops (tornado allows a loop to be closed while running, but asyncio doesn't). This looks like the difference between AsyncIOMainLoop and AsyncIOLoop. I think you almost always want to use AsyncIOMainLoop. Tornado 5.0 will default to asyncio when available and include other changes to reduce this friction.

All 5 comments

Looks like with Python 3.6.0+ (3.6:ea87e00a3e89, Dec 28 2016, 12:22:26) works

Just realized that it has also happened with aioredis.

Looks like to be caused by the same reason as #1543

I think this is a bit different from #1543. That issue is about the differences between the tornado and asyncio event loops (tornado allows a loop to be closed while running, but asyncio doesn't). This looks like the difference between AsyncIOMainLoop and AsyncIOLoop. I think you almost always want to use AsyncIOMainLoop. Tornado 5.0 will default to asyncio when available and include other changes to reduce this friction.

The changes to unify the two event loops for Tornado 5.0 are complete (in the master branch). Now this example should just work without doing anything to configure the IOLoop.

Was this page helpful?
0 / 5 - 0 ratings