o
    Qœ_¬  ã                   @   sž  d Z ddlZddlmZ ddlZddlZddlZddlmZ ddl	Z	ddl	m
Z
mZmZmZmZ e	 d¡ZG dd„ deƒZejZejefZd	e
d
efdd„ZG dd„ dejƒZeƒ Zde
de
d
efdd„Zeƒ Zd*dd„Zddded
dfdd„Zddded
dfdd„Z ddd eee! ee eej" f d
dfd!d"„Z#e	j$dd#d$ed#gdf d
dfd%d&„ƒZ%e	j$ddd$edgdf d
dfd'd&„ƒZ%ddd$ed( d
dfd)d&„Z%dS )+a¨  Utilities for working with ``Future`` objects.

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
implementation, applications rarely need to interact with it
directly.

é    N)Úfutures)Úapp_log)ÚAnyÚCallableÚOptionalÚTupleÚUnionÚ_Tc                   @   s   e Zd ZdS )ÚReturnValueIgnoredErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú4/usr/lib/python3/dist-packages/tornado/concurrent.pyr
   *   s    r
   ÚxÚreturnc                 C   s
   t | tƒS ©N)Ú
isinstanceÚFUTURES)r   r   r   r   Ú	is_future4   s   
r   c                   @   sB   e Zd Zdedef dededdfdd„Zdd
eddfdd„ZdS )ÚDummyExecutorÚfn.ÚargsÚkwargsr   úfutures.Future[_T]c                 O   sF   t  ¡ }zt|||i |¤Žƒ W |S  ty"   t|t ¡ ƒ Y |S w r   )r   ÚFutureÚ"future_set_result_unless_cancelledÚ	ExceptionÚfuture_set_exc_infoÚsysÚexc_info)Úselfr   r   r   Úfuturer   r   r   Úsubmit9   s   þþzDummyExecutor.submitTÚwaitNc                 C   ó   d S r   r   )r!   r$   r   r   r   ÚshutdownC   s   zDummyExecutor.shutdown)T)	r   r   r   r   r	   r   r#   Úboolr&   r   r   r   r   r   8   s    
ÿÿÿ
þ
r   r   r   c                     sd   dt dt dtf f‡ fdd„}| rˆ rtdƒ‚t| ƒdkr#|| d ƒS t| ƒdkr0td	t| ƒƒ‚|S )
aE  Decorator to run a synchronous method asynchronously on an executor.

    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.

    .. versionchanged:: 6.0

       The ``callback`` argument was removed.
    r   r   .c              	      s:   ˆ  dd¡‰ t ˆ¡dtdtdtdtf‡ ‡fdd„ƒ}|S )NÚexecutorr!   r   r   r   c                    s4   t ƒ }t| ˆ ƒjˆ| g|¢R i |¤Ž}t||ƒ |S r   )r   Úgetattrr#   Úchain_future)r!   r   r   Úasync_futureÚconc_future©r(   r   r   r   Úwrapperw   s    
zCrun_on_executor.<locals>.run_on_executor_decorator.<locals>.wrapper)ÚgetÚ	functoolsÚwrapsr   r   )r   r.   ©r   r-   r   Úrun_on_executor_decoratort   s   "z2run_on_executor.<locals>.run_on_executor_decoratorz*cannot combine positional and keyword argsé   r   zexpected 1 argument, got %d)r   r   Ú
ValueErrorÚlen)r   r   r3   r   r2   r   Úrun_on_executorJ   s   *r7   Úaú
Future[_T]Úbc                    sH   d	‡ ‡fdd„}t ˆ tƒrtˆ |ƒ dS ddlm} | ¡  ˆ |¡ dS )
aj  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`.

    r"   r9   r   Nc                    sp   | ˆ u sJ ‚ˆ  ¡ rd S tˆ dƒr ˆ  ¡ d ur tˆˆ  ¡ ƒ d S ˆ  ¡ d ur/ˆ ˆ  ¡ ¡ d S ˆ ˆ  ¡ ¡ d S )Nr    )ÚdoneÚhasattrr    r   Ú	exceptionÚset_exceptionÚ
set_resultÚresult)r"   ©r8   r:   r   r   Úcopy™   s   zchain_future.<locals>.copyr   )ÚIOLoop)r"   r9   r   N)r   r   Úfuture_add_done_callbackÚtornado.iolooprC   ÚcurrentÚ
add_future)r8   r:   rB   rC   r   rA   r   r*   Œ   s
   
r*   r"   z%Union[futures.Future[_T], Future[_T]]Úvaluec                 C   s   |   ¡ s|  |¡ dS dS )zÐSet the given ``value`` as the `Future`'s result, if not cancelled.

    Avoids ``asyncio.InvalidStateError`` when calling ``set_result()`` on
    a cancelled `asyncio.Future`.

    .. versionadded:: 5.0
    N)Ú	cancelledr?   )r"   rH   r   r   r   r   ­   s   
ÿr   Úexcc                 C   s(   |   ¡ s|  |¡ dS tjd|d� dS )a©  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

    z$Exception after Future was cancelled)r    N)rI   r>   r   Úerror)r"   rJ   r   r   r   Ú%future_set_exception_unless_cancelled»   s   rL   r    c                 C   s&   |d du r
t dƒ‚t| |d ƒ dS )a�  Set the given ``exc_info`` as the `Future`'s exception.

    Understands both `asyncio.Future` and the extensions in older
    versions of Tornado to enable better tracebacks on Python 2.

    .. versionadded:: 5.0

    .. versionchanged:: 6.0

       If the future is already cancelled, this function is a no-op.
       (previously ``asyncio.InvalidStateError`` would be raised)

    r4   Nz,future_set_exc_info called with no exception)r   rL   )r"   r    r   r   r   r   Ñ   s   r   r   Úcallbackc                 C   r%   r   r   ©r"   rM   r   r   r   rD   é   ó   rD   c                 C   r%   r   r   rN   r   r   r   rD   ð   rO   ).Nc                 C   s"   |   ¡ r
|| ƒ dS |  |¡ dS )aL  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
    N)r;   Úadd_done_callbackrN   r   r   r   rD   ÷   s   )r8   r9   r:   r9   r   N)&Ú__doc__ÚasyncioÚ
concurrentr   r0   r   ÚtypesÚtornado.logr   Útypingr   r   r   r   r   ÚTypeVarr	   r   r
   r   r   r'   r   ÚExecutorr   Údummy_executorr7   ÚobjectÚ
_NO_RESULTr*   r   ÚBaseExceptionrL   ÚtypeÚTracebackTyper   ÚoverloadrD   r   r   r   r   Ú<module>   s‚   

?
!ÿÿ
þÿÿ
þÿÿþ
ûÿÿþÿÿþÿÿþ