【问题标题】:Get result from MotorEngine async query从 MotorEngine 异步查询中获取结果
【发布时间】:2014-09-23 10:53:19
【问题描述】:

为了异步数据库访问,我正在尝试在我的 Tornado 应用程序中将 MongoEngine 与 MotorEngine 切换,但到目前为止我一无所获。

query

@gen.coroutine
    def get_all_users(self):
        users = yield User.objects.find_all()

handler

class IUser(BaseHandler):
    @asynchronous
    @gen.engine
    def get(self,userId=None, *args, **kwargs):
        try:
            userMethods = UserMethods()
            sessionId = self.request.headers.get('sessionId')
            ret = userMethods.get_all_users()
        except Exception as ex:
            print str(ex)

        self.finish()

当我打印ret 变量时,它显示<tornado.concurrent.Future object at 0x7fb0236fe450>。如果我尝试打印ret.result(),我将一事无成。

感谢任何帮助,因为我正在努力解决我猜想的一切......

【问题讨论】:

    标签: python tornado tornado-motor


    【解决方案1】:

    get_all_users 需要以某种方式返回其值。在 Python 2.6 或 2.7 中,生成器不允许使用“return”语句,因此协程有一个特殊的“Return”异常:

    @gen.coroutine
    def get_all_users(self):
        users = yield User.objects.find_all()
        raise gen.Return(users)
    

    在 Python 3.3 及更高版本中,您可以简单地“返回用户”。

    现在在“get”中,调用“get_all_users”只会给你一个待处理的 Future,而不是一个值。你必须等待 Future 通过 yield 来解析一个值:

    ret = yield userMethods.get_all_users()
    

    有关从协程调用协程的更多信息,请参阅我的"Refactoring Tornado Coroutines"

    顺便说一句,你可以只用“gen.coroutine”来装饰“get”,它比“asynchronous”和“gen.engine”更现代,但任何一种风格都可以。

    【讨论】:

    • A+ 答案,一如既往。谢谢。
    【解决方案2】:

    只是一个建议。如果你想避免每次使用它的方法时都创建一个 userMethods 的实例:

    userMethods = UserMethods()
    

    你可以在声明之前使用@classmethod装饰器:

    class UserMethods():
        pass
    
    @classmethod
    @tornado.gen.coroutine
    def get_all_users(self):
        users = yield User.objects.find_all()
        raise gen.Return(users)
    
    ## class IUser
    ...
    
    try:
        # userMethods = UserMethods()   --not necesary now--
        sessionId = self.request.headers.get('sessionId')
        ret = yield userMethods.get_all_users()
    except Exception as ex:
        print str(ex)
    ...
    

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 2018-02-04
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      相关资源
      最近更新 更多