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¶ A
Futureencapsulates the result of an asynchronous operation. In synchronous applicationsFuturesare used to wait for the result from a thread or process pool; in Tornado they are normally used withIOLoop.add_futureor by yielding them in agen.coroutine.If the
concurrent.futurespackage is available,tornado.concurrent.Futureis simply an alias forconcurrent.futures.Future. Otherwise, we support the same interface with a few limitations:- It is an error to call
resultorexceptionbefore theFuturehas completed. - Cancellation is not supported.
-
result()¶ If the operation succeeded, return its result. If it failed, re-raise its exception.
-
exception()¶ If the operation raised an exception, return the
Exceptionobject. Otherwise returns None.
-
add_done_callback(fn)¶ Attaches the given callback to the
Future. It will be invoked with theFutureas its argument when it has finished running and its result is available. In Tornado consider usingIOLoop.add_futureinstead of callingadd_done_callbackdirectly.
- It is an error to call
-
class
tornado.concurrent.TracebackFuture[source]¶ Subclass of
Futurewhich can store a traceback with exceptions.The traceback is automatically available in Python 3, but in the Python 2 futures backport this information is discarded.
-
set_exc_info(exc_info)[source]¶ Traceback-aware replacement for
set_exception.
-
-
tornado.concurrent.run_on_executor(fn)[source]¶ Decorator to run a synchronous method asynchronously on an executor.
The decorated method may be called with a
callbackkeyword argument and returns a future.
-
tornado.concurrent.return_future(f)[source]¶ Decorator to make a function that returns via callback return a
Future.The wrapped function should take a
callbackkeyword 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 theStackContextand passed along to theFuture).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 surroundingStackContext.If no callback is given, the caller should use the
Futureto wait for the function to complete (perhaps by yielding it in agen.enginefunction, or passing it toIOLoop.add_future).Usage:
@return_future def future_func(arg1, arg2, callback): # Do stuff (possibly asynchronous) callback(result) @gen.engine def caller(callback): yield future_func(arg1, arg2) callback()Note that
@return_futureand@gen.enginecan be applied to the same function, provided@return_futureappears first. However, consider using@gen.coroutineinstead of this combination.