tornado.concurrent — Work with threads and futures

Utilities for working with threads and Futures.

Futures are a pattern for concurrent programming introduced in Python 3.2 in the concurrent.futures package (this package has also been backported to older versions of Python and can be installed with pip install futures). Tornado will use concurrent.futures.Future if it is available; otherwise it will use a compatible class defined in this module.

class tornado.concurrent.Future[source]

A Future encapsulates the result of an asynchronous operation. In synchronous applications Futures are used to wait for the result from a thread or process pool; in Tornado they are normally used with IOLoop.add_future or by yielding them in a gen.coroutine.

If the concurrent.futures package is available, tornado.concurrent.Future is simply an alias for concurrent.futures.Future. Otherwise, we support the same interface with a few limitations:

  • It is an error to call result or exception before the Future has completed.
  • Cancellation is not supported.

If the operation succeeded, return its result. If it failed, re-raise its exception.


If the operation raised an exception, return the Exception object. Otherwise returns None.


Attaches the given callback to the Future. It will be invoked with the Future as its argument when it has finished running and its result is available. In Tornado consider using IOLoop.add_future instead of calling add_done_callback directly.


Returns True if the future has finished running and its result and exception methods are available.

class tornado.concurrent.Future[source]

Placeholder for an asynchronous result.

Similar to concurrent.futures.Future, but not thread-safe (and therefore faster for use with single-threaded event loops.

In addition to exception and set_exception, methods exc_info and set_exc_info are supported to capture tracebacks in Python 2. The traceback is automatically available in Python 3, but in the Python 2 futures backport this information is discarded. This functionality was previously available in a separate class TracebackFuture, which is now a deprecated alias for this class.


Traceback-aware replacement for set_exception.


alias of Future


alias of Future


Decorator to run a synchronous method asynchronously on an executor.

The decorated method may be called with a callback keyword argument and returns a future.

This decorator should be used only on methods of objects with attributes executor and io_loop.


Decorator to make a function that returns via callback return a Future.

The wrapped function should take a callback keyword argument and invoke it with one argument when it has finished. To signal failure, the function can simply raise an exception (which will be captured by the StackContext and passed along to the Future).

From the caller’s perspective, the callback argument is optional. If one is given, it will be invoked when the function is complete with Future.result() as an argument. If the function fails, the callback will not be run and an exception will be raised into the surrounding StackContext.

If no callback is given, the caller should use the Future to wait for the function to complete (perhaps by yielding it in a gen.engine function, or passing it to IOLoop.add_future).


def future_func(arg1, arg2, callback):
    # Do stuff (possibly asynchronous)

def caller(callback):
    yield future_func(arg1, arg2)

Note that @return_future and @gen.engine can be applied to the same function, provided @return_future appears first. However, consider using @gen.coroutine instead of this combination.

tornado.concurrent.chain_future(a, b)[source]

Chain two futures together so that when one completes, so does the other.

The result (success or failure) of a will be copied to b.

Previous topic

tornado.autoreload — Automatically detect code changes in development

Next topic

tornado.httputil — Manipulate HTTP headers and URLs

This Page