当前位置: 首页 > 文档资料 > Tornado 用户手册 >

tornado.concurrent — Work with threads and futures

优质
小牛编辑
131浏览
2023-12-01

Utilities for working with threads and Futures.

Futures are a pattern for concurrent programming introduced in Python 3.2 in the package. This package defines a mostly-compatible class designed for use from coroutines, as well as some utility functions for interacting with the package.

class tornado.concurrent.Future

Placeholder for an asynchronous result.

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 or by yielding them in a .

is similar to , 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.

在 4.0 版更改: is always a thread-unsafe Future with support for the exc_info methods. Previously it would be an alias for the thread-safe if that package was available and fall back to the thread-unsafe implementation if it was not.

在 4.1 版更改: If a contains an error but that error is never observed (by calling result(), exception(), or exc_info()), a stack trace will be logged when the is garbage collected. This normally indicates an error in the application, but in cases where it results in undesired logging it may be necessary to suppress the logging by ensuring that the exception is observed: f.add_done_callback(lambda f: f.exception()).

Consumer methods

Future.result(timeout=None)

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

This method takes a timeout argument for compatibility with but it is an error to call it before the is done, so the timeout is never used.

Future.exception(timeout=None)

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

This method takes a timeout argument for compatibility with but it is an error to call it before the is done, so the timeout is never used.

Future.exc_info()

Returns a tuple in the same format as or None.

4.0 新版功能.

Future.add_done_callback(fn)

Attaches the given callback to the .

It will be invoked with the as its argument when the Future has finished running and its result is available. In Tornado consider using instead of calling directly.

Future.done()

Returns True if the future has finished running.

Future.running()

Returns True if this operation is currently running.

Future.cancel()

Cancel the operation, if possible.

Tornado Futures do not support cancellation, so this method always returns False.

Future.cancelled()

Returns True if the operation has been cancelled.

Tornado Futures do not support cancellation, so this method always returns False.

Producer methods

Future.set_result(result)

Sets the result of a Future.

It is undefined to call any of the set methods more than once on the same object.

Future.set_exception(exception)

Sets the exception of a Future.

Future.set_exc_info(exc_info)

Sets the exception information of a Future.

Preserves tracebacks on Python 2.

4.0 新版功能.

tornado.concurrent.FUTURES

的别名

tornado.concurrent.run_on_executor(*args, **kwargs)

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.

The and executor to be used are determined by the io_loop and executor attributes of self. To use different attributes, pass keyword arguments to the decorator:

@run_on_executor(executor='_thread_pool')
def foo(self):
    pass

在 4.2 版更改: Added keyword arguments to use alternative attributes.

tornado.concurrent.return_future(f)

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

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 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 as an argument. If the function fails, the callback will not be run and an exception will be raised into the surrounding .

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

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_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)

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, unless b has already been completed or cancelled by the time a finishes.