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?
spawn_callbackis 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 awaitdo_findas a normal coroutine (and don't touch_auto_finish):