簡體   English   中英

龍卷風,在回調函數中訪問其他數據?

[英]Tornado, Accessing additional data in callback function?

我剛剛使用Tornado和asyncmongo啟動了一個項目。

我有一個帶有異步方法的處理程序。 在內部,我正在向mongo查詢一些單詞:

@tornado.web.asynchronous
def get(self):
    word = self.get_argument('word', None)
    if not word:
        self.write('{}')
        self.finish()
    self.db.spanish_dict.find({'$or': [{'word': word}, {'stem': word}]},
                              callback=self._on_response)


def _on_response(self, response, error):
   # need to sort response by relevancy

在我的回調方法中,我需要使用原始單詞對mongo結果進行准確排序。

我發現這篇文章使用functools.partial通過允許我將其他參數傳遞給回調方法來完成此任務

我想知道是否會對在get方法中設置實例屬性並在_on_response訪問它產生任何不利影響? 謝謝

@tornado.web.asynchronous
def get(self):
    word = self.get_argument('word', None)
    if not word:
        self.write('{}')
        self.finish()
    self.word = word
    self.db.spanish_dict.find({'$or': [{'word': word}, {'stem': word}]},
                              callback=self._on_response)


def _on_response(self, response, error):
   # need to sort response by relevancy
   # will self.word always be accurate?
   self.word

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM