简体   繁体   English

如何保持龙卷风呼叫异步?

[英]How to keep tornado call async?

While doing an async mongodb query like the one in the below class how is this call really non-blocking if I still have access to an argument like self.get_argument("ip_address") inside the callback function? 在执行类似以下类中的异步mongodb查询时,如果我仍然可以在回调函数中访问诸如self.get_argument(“ ip_address”)之类的参数,则此调用真的不会阻塞吗? Or should I not access to the argument like this to keep the call async? 还是我不应该访问这样的参数来保持通话异步?

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")

The instance self referenced in the callback function will be hanging around until the end of the callback function, therefore self.arguments will always be available inside _valid_app . 在回调函数中self引用的实例将一直挂在回调函数的末尾,因此self.arguments将始终在_valid_app可用。

Maybe you can be confused by what would happen if another request to the same handler were made during the async call to Mongo. 如果在对Mongo的异步调用期间向同一处理程序提出另一个请求,将会发生什么,您可能会感到困惑。 This would not be a problem because, for any new request a new instance of MainHandler is created, not interfering with the previous one. 这不会有问题,因为对于任何新请求, MainHandler创建一个新的MainHandler实例,而不干扰前一个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM