How to use motor in tornado?

103 Views Asked by At

class Global(RequestHandler):
    async def post(self):
        self._auto_finish = False #关闭长链接
        IOLoop.current().spawn_callback(self.do_find)
    async def do_find(self):
        if self.settings["admin"] != 1:
            cursor = db.find(projection={'_id': 0})
            documents = [document for document in (await cursor.to_list(length=100))]
            print(documents)
        self.write("ok")
        self.finish()

tornado==6.0.4 motor==2.1 This is the code I wrote according to the official website of the motor. When the long link is turned on, the motor programming is synchronously queried; when the long link is turned off, the motor can be asynchronous, but it cannot return any value. The response status code is 200. Excuse me, how are tornado and motor used?

1

There are 1 best solutions below

0
Ben Darnell On

spawn_callback is for "fire and forget" tasks that execute after you've already returned a response to the caller. That's not what you want here; you want to call and await do_find as a normal coroutine (and don't touch _auto_finish):

async def post(self):
    await self.do_find()