teleport/server/www/packages/packages-windows/x86/tornado/concurrent.py

265 lines
8.0 KiB
Python
Raw Normal View History

2018-09-23 19:29:42 +00:00
#
# Copyright 2012 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Utilities for working with ``Future`` objects.
2020-06-04 16:58:17 +00:00
Tornado previously provided its own ``Future`` class, but now uses
`asyncio.Future`. This module contains utility functions for working
with `asyncio.Future` in a way that is backwards-compatible with
Tornado's old ``Future`` implementation.
While this module is an important part of Tornado's internal
2018-09-23 19:29:42 +00:00
implementation, applications rarely need to interact with it
directly.
2020-06-04 16:58:17 +00:00
2018-09-23 19:29:42 +00:00
"""
2020-06-04 16:58:17 +00:00
import asyncio
from concurrent import futures
2018-09-23 19:29:42 +00:00
import functools
import sys
2020-06-04 16:58:17 +00:00
import types
2018-09-23 19:29:42 +00:00
from tornado.log import app_log
2020-06-04 16:58:17 +00:00
import typing
from typing import Any, Callable, Optional, Tuple, Union
2018-09-23 19:29:42 +00:00
2020-06-04 16:58:17 +00:00
_T = typing.TypeVar("_T")
2018-09-23 19:29:42 +00:00
class ReturnValueIgnoredError(Exception):
2020-06-04 16:58:17 +00:00
# No longer used; was previously used by @return_future
2018-09-23 19:29:42 +00:00
pass
2020-06-04 16:58:17 +00:00
Future = asyncio.Future
2018-09-23 19:29:42 +00:00
2020-06-04 16:58:17 +00:00
FUTURES = (futures.Future, Future)
2018-09-23 19:29:42 +00:00
2020-06-04 16:58:17 +00:00
def is_future(x: Any) -> bool:
2018-09-23 19:29:42 +00:00
return isinstance(x, FUTURES)
2020-06-04 16:58:17 +00:00
class DummyExecutor(futures.Executor):
def submit(
self, fn: Callable[..., _T], *args: Any, **kwargs: Any
) -> "futures.Future[_T]":
future = futures.Future() # type: futures.Future[_T]
2018-09-23 19:29:42 +00:00
try:
future_set_result_unless_cancelled(future, fn(*args, **kwargs))
except Exception:
future_set_exc_info(future, sys.exc_info())
return future
2020-06-04 16:58:17 +00:00
def shutdown(self, wait: bool = True) -> None:
2018-09-23 19:29:42 +00:00
pass
dummy_executor = DummyExecutor()
2020-06-04 16:58:17 +00:00
def run_on_executor(*args: Any, **kwargs: Any) -> Callable:
2018-09-23 19:29:42 +00:00
"""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 executor to be used is determined by the ``executor``
attributes of ``self``. To use a different attribute name, pass a
keyword argument to the decorator::
@run_on_executor(executor='_thread_pool')
def foo(self):
pass
This decorator should not be confused with the similarly-named
`.IOLoop.run_in_executor`. In general, using ``run_in_executor``
when *calling* a blocking method is recommended instead of using
this decorator when *defining* a method. If compatibility with older
versions of Tornado is required, consider defining an executor
and using ``executor.submit()`` at the call site.
.. versionchanged:: 4.2
Added keyword arguments to use alternative attributes.
.. versionchanged:: 5.0
Always uses the current IOLoop instead of ``self.io_loop``.
.. versionchanged:: 5.1
Returns a `.Future` compatible with ``await`` instead of a
`concurrent.futures.Future`.
.. deprecated:: 5.1
The ``callback`` argument is deprecated and will be removed in
6.0. The decorator itself is discouraged in new code but will
not be removed in 6.0.
2020-06-04 16:58:17 +00:00
.. versionchanged:: 6.0
The ``callback`` argument was removed.
2018-09-23 19:29:42 +00:00
"""
2020-06-04 16:58:17 +00:00
# Fully type-checking decorators is tricky, and this one is
# discouraged anyway so it doesn't have all the generic magic.
def run_on_executor_decorator(fn: Callable) -> Callable[..., Future]:
2018-09-23 19:29:42 +00:00
executor = kwargs.get("executor", "executor")
@functools.wraps(fn)
2020-06-04 16:58:17 +00:00
def wrapper(self: Any, *args: Any, **kwargs: Any) -> Future:
async_future = Future() # type: Future
2018-09-23 19:29:42 +00:00
conc_future = getattr(self, executor).submit(fn, self, *args, **kwargs)
chain_future(conc_future, async_future)
return async_future
2020-06-04 16:58:17 +00:00
2018-09-23 19:29:42 +00:00
return wrapper
2020-06-04 16:58:17 +00:00
2018-09-23 19:29:42 +00:00
if args and kwargs:
raise ValueError("cannot combine positional and keyword args")
if len(args) == 1:
return run_on_executor_decorator(args[0])
elif len(args) != 0:
raise ValueError("expected 1 argument, got %d", len(args))
return run_on_executor_decorator
_NO_RESULT = object()
2020-06-04 16:58:17 +00:00
def chain_future(a: "Future[_T]", b: "Future[_T]") -> None:
2018-09-23 19:29:42 +00:00
"""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.
.. versionchanged:: 5.0
Now accepts both Tornado/asyncio `Future` objects and
`concurrent.futures.Future`.
"""
2020-06-04 16:58:17 +00:00
def copy(future: "Future[_T]") -> None:
2018-09-23 19:29:42 +00:00
assert future is a
if b.done():
return
2020-06-04 16:58:17 +00:00
if hasattr(a, "exc_info") and a.exc_info() is not None: # type: ignore
future_set_exc_info(b, a.exc_info()) # type: ignore
2018-09-23 19:29:42 +00:00
elif a.exception() is not None:
b.set_exception(a.exception())
else:
b.set_result(a.result())
2020-06-04 16:58:17 +00:00
2018-09-23 19:29:42 +00:00
if isinstance(a, Future):
future_add_done_callback(a, copy)
else:
# concurrent.futures.Future
from tornado.ioloop import IOLoop
2020-06-04 16:58:17 +00:00
2018-09-23 19:29:42 +00:00
IOLoop.current().add_future(a, copy)
2020-06-04 16:58:17 +00:00
def future_set_result_unless_cancelled(
future: "Union[futures.Future[_T], Future[_T]]", value: _T
) -> None:
2018-09-23 19:29:42 +00:00
"""Set the given ``value`` as the `Future`'s result, if not cancelled.
2020-06-04 16:58:17 +00:00
Avoids ``asyncio.InvalidStateError`` when calling ``set_result()`` on
2018-09-23 19:29:42 +00:00
a cancelled `asyncio.Future`.
.. versionadded:: 5.0
"""
if not future.cancelled():
future.set_result(value)
2020-06-04 16:58:17 +00:00
def future_set_exception_unless_cancelled(
future: "Union[futures.Future[_T], Future[_T]]", exc: BaseException
) -> None:
"""Set the given ``exc`` as the `Future`'s exception.
If the Future is already canceled, logs the exception instead. If
this logging is not desired, the caller should explicitly check
the state of the Future and call ``Future.set_exception`` instead of
this wrapper.
Avoids ``asyncio.InvalidStateError`` when calling ``set_exception()`` on
a cancelled `asyncio.Future`.
.. versionadded:: 6.0
"""
if not future.cancelled():
future.set_exception(exc)
else:
app_log.error("Exception after Future was cancelled", exc_info=exc)
def future_set_exc_info(
future: "Union[futures.Future[_T], Future[_T]]",
exc_info: Tuple[
Optional[type], Optional[BaseException], Optional[types.TracebackType]
],
) -> None:
2018-09-23 19:29:42 +00:00
"""Set the given ``exc_info`` as the `Future`'s exception.
2020-06-04 16:58:17 +00:00
Understands both `asyncio.Future` and the extensions in older
versions of Tornado to enable better tracebacks on Python 2.
2018-09-23 19:29:42 +00:00
.. versionadded:: 5.0
2020-06-04 16:58:17 +00:00
.. versionchanged:: 6.0
If the future is already cancelled, this function is a no-op.
(previously ``asyncio.InvalidStateError`` would be raised)
2018-09-23 19:29:42 +00:00
"""
2020-06-04 16:58:17 +00:00
if exc_info[1] is None:
raise Exception("future_set_exc_info called with no exception")
future_set_exception_unless_cancelled(future, exc_info[1])
@typing.overload
def future_add_done_callback(
future: "futures.Future[_T]", callback: Callable[["futures.Future[_T]"], None]
) -> None:
pass
@typing.overload # noqa: F811
def future_add_done_callback(
future: "Future[_T]", callback: Callable[["Future[_T]"], None]
) -> None:
pass
2018-09-23 19:29:42 +00:00
2020-06-04 16:58:17 +00:00
def future_add_done_callback( # noqa: F811
future: "Union[futures.Future[_T], Future[_T]]", callback: Callable[..., None]
) -> None:
2018-09-23 19:29:42 +00:00
"""Arrange to call ``callback`` when ``future`` is complete.
``callback`` is invoked with one argument, the ``future``.
If ``future`` is already done, ``callback`` is invoked immediately.
This may differ from the behavior of ``Future.add_done_callback``,
which makes no such guarantee.
.. versionadded:: 5.0
"""
if future.done():
callback(future)
else:
future.add_done_callback(callback)