Running an async background task in Tornado

Yuval Adam picture Yuval Adam · Feb 27, 2014 · Viewed 14.5k times · Source

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?

Answer

Ben Darnell picture Ben Darnell · Feb 28, 2014

Update: Since Tornado 4.0 (July 2014), the below functionality is available in the IOLoop.spawn_callback method.

Unfortunately it's kind of tricky. You need to both detach the background task from the current request (so that a failure in the background task doesn't result in a random exception thrown into the request) and ensure that something is listening to the background task's result (to log its errors if nothing else). This means something like this:

from tornado.ioloop import IOLoop
from tornado.stack_context import run_in_stack_context, NullContext
IOLoop.current().add_future(run_in_stack_context(NullContext(), self._background_task),
                            lambda f: f.result())

Something like this will probably be added to tornado itself in the future.