2016-04-30 1 views
2

取得メソッドとポストメソッドでリクエストを処理するハンドラがあります。自分自身のカスタムデコレータで認証を使用します。トルネード自体ではありません@ tornado.web.authenticated decorator 。私のカスタムデコレータでは、ユーザを識別するためにdbをクエリする必要がありますが、竜巻のdbクエリは@gen.coroutineと非同期です。トルネードでdb操作を行うときにカスタムデコレータでcoroutineを使用する方法

私のコードは次のとおりです。

handlers.py。

@account.utils.authentication 
    @gen.coroutine 
    def get(self, page): 

アカウント/ utils.py:

@tornado.gen.coroutine 
def authentication(fun): 
    def test(self,*args, **kwargs ): 
     print(self) 
     db = self.application.settings['db'] 
     result = yield db.user.find() 
     r = yield result.to_list(None) 
     print(r) 
    return test 

が、それをアクセスしたときerrosが発生しました:

Traceback (most recent call last): File "/Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/web.py", line 1443, in _execute result = method(*self.path_args, **self.path_kwargs) TypeError: 'Future' object is not callable

誰もが独自のデコレータを書くために正しく方法は何か、前にこれを満たすことができますasync db操作で認証するには?事前に感謝〜

答えて

4

デコレータは同期する必要があります。それはコルーチンであるを返す関数です。あなたは変更する必要があります。

@tornado.gen.coroutine 
def authentication(fun): 
    def test(self, *args, **kwargs): 
     ... 
    return test 

へ:

def authentication(fun): 
    @tornado.gen.coroutine # note 
    def test(self, *args, **kwargs): 
     ... 
    return test 
関連する問題