[英]How to keep tornado call async?
在執行類似以下類中的異步mongodb查詢時,如果我仍然可以在回調函數中訪問諸如self.get_argument(“ ip_address”)之類的參數,則此調用真的不會阻塞嗎? 還是我不應該訪問這樣的參數來保持通話異步?
class MainHandler(tornado.web.RequestHandler):
def get(self):
app_key = self.get_argument("app_key")
#async call to mongodb. call _valid_app afterwards
db.apps.find_one({'app_key': app_key}, callback=self._valid_app);
def _valid_app(self, response, error):
if error:
raise tornado.web.HTTPError(500)
if response:
ip_address = self.get_argument("ip_address")
#rest of the code
else:
print("invalid app_key")
在回調函數中self
引用的實例將一直掛在回調函數的末尾,因此self.arguments
將始終在_valid_app
可用。
如果在對Mongo的異步調用期間向同一處理程序提出另一個請求,將會發生什么,您可能會感到困惑。 這不會有問題,因為對於任何新請求, MainHandler
創建一個新的MainHandler
實例,而不干擾前一個實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.