In a simple async case, handler might look like:
@tornado.web.authenticated
@tornado.web.asynchronous
def post(self):
AsyncHTTPClient().fetch("http://api.example.com/", self.on_post_response)
def on_post_response(self, response):
self.render("template.html", status=response.error)
However, I have come to a point when I need to perform two async operations (fetching remote rest api, and then sending mail with the results) before returning to the client.
I wonder if there is a "buit-in" way to do this, e.g. by adding callbacks to a queue (like ioloop.add_callback
) or do I have to compose a custom object which will manage those tasks and their state and call it from post
.
Have you considered the following approach?
Or using tornado.gen: