chaining asynchronous operations before writing to client (python - tornado)

两盒软妹~` 提交于 2019-12-21 21:28:28

问题


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(...)


来源:https://stackoverflow.com/questions/9377902/chaining-asynchronous-operations-before-writing-to-client-python-tornado

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!