Search code examples
asynchronoustornadopython-3.5

How to call extra async method in Tornado?


I want to call an async method from other libraries in tornado, like so:

class Database:
    async def find_info(user_id):
        pass

class TestClass(tornado.web.RequestHandler):
    def get(self, id):
        db = Database()
        user = yield db.find_info(user_id=id)
        return self.write(user.username)

But it goes to something like sleeping mode and I'll never get any result.


Solution

  • Which other libraries? Most async functions are written for a particular event loop (Tornado, asyncio, Twisted, etc). Different event loops don't cooperate unless you ask them to. You probably want to enable Tornado/asyncio interoperability with tornado.platform.asyncio.AsyncIOMainLoop