首页 > 解决方案 > 如何在龙卷风中使用电机?

问题描述

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 这是我根据motor官网写的代码。开启长链接时,同步查询电机编程;当长链接关闭时,电机可以异步,但不能返回任何值。响应状态码是200。请问,龙卷风和马达是怎么用的?

标签: pythonpython-3.xtornadopython-asynciotornado-motor

解决方案


spawn_callback用于在您已经向调用者返回响应之后执行的“即发即弃”任务。这不是你想要的。您想像普通协程一样调用和等待do_find(并且不要触摸_auto_finish):

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

推荐阅读