chaining asynchronous operations before writing to

2019-05-24 07:41发布

问题:

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.

回答1:

Have you considered the following approach?

@tornado.web.authenticated
@tornado.web.asynchronous
def post(self):
    async_fetch(..., self._on_fetch_response)

def _on_fetch_response(self, response):
    async_mail(response, self._on_mail_response)

def _on_mail_response(self, response):
    self.render(...) # render() automatically calls self.finish()

Or using tornado.gen:

@asynchronous
@gen.engine
def post(self):
    fetch_response = yield gen.Task(async_fetch, ...)
    mail_response = yield gen.Task(async_mail, ...)
    self.render(...)