【发布时间】:2014-03-31 16:30:49
【问题描述】:
阅读 Tornado 文档,很清楚如何调用异步函数返回响应:
class GenAsyncHandler(RequestHandler):
@gen.coroutine
def get(self):
http_client = AsyncHTTPClient()
response = yield http_client.fetch("http://example.com")
do_something_with_response(response)
self.render("template.html")
缺乏的是如何异步调用与当前请求无关的后台任务:
class GenAsyncHandler(RequestHandler):
@gen.coroutine
def _background_task():
pass # do lots of background stuff
@gen.coroutine
def get(self):
_dont_care = yield self._background_task()
self.render("template.html")
这段代码应该可以工作,除了它同步运行并且请求在它上面等待直到它完成。
什么是异步调用这个任务,同时立即返回当前请求的正确方式?
【问题讨论】:
标签: python asynchronous tornado