Reading the Tornado documentation, it's very clear how to call an async function to return a response:
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")
What's lacking is how should a call be made asynchronously to a background task that has no relevance to the current request:
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")
This code would be expected to work, except that it runs synchronously and the request waits on it until it's finished.
What is the right way to asynchronously call this task, while immediately returning the current request?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…