B
    h                 @   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e
ed	d
dZG dd dejZe Ze
e
edddZe ZddddddZdeddddZdeddddZ deee! ee eej" f ddddZ#e	j$dedgdf dd d!d"Z%e	j$dedgdf dd d#d"Z%ded$ dd 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   OC:\Users\sanjo\AppData\Local\Qlobot\Launcher\ext_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               @   s:   e Zd Zedef eeddddZdeddd	d
ZdS )DummyExecutor.zfutures.Future[_T])fnargskwargsr   c             O   sD   t  }yt|||| W n" tk
r>   t|t  Y nX |S )N)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.submitTN)waitr   c             C   s   d S )Nr   )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   r   c                 sb   t t dtf d fdd}| r, r,tdt| dkrD|| d S t| dkr^tdt| |S )	a  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.

    .. versionchanged:: 6.0

       The ``callback`` argument was removed.
    .)r   r   c                s4    dd tttttd fdd}|S )Nexecutor)r   r   r   r   c                s.   t  }t|  j| f||}t|| |S )N)r   getattrr!   chain_future)r   r   r   Zasync_futureZconc_future)r%   r   r   r   wrapperx   s    
zCrun_on_executor.<locals>.run_on_executor_decorator.<locals>.wrapper)get	functoolswrapsr   r   )r   r(   )r   )r%   r   r   run_on_executor_decoratoru   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   r,   r   )r   r   run_on_executorJ   s    +r0   z
Future[_T])abr   c                sL   ddd fdd}t  tr,t | n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`.

    z
Future[_T]N)r    r   c                sl   |  kst  rd S t dr>  d k	r>t   n*  d k	rZ   n   d S )Nr   )	AssertionErrordonehasattrr   r   	exceptionset_exception
set_resultresult)r    )r1   r2   r   r   copy   s    zchain_future.<locals>.copyr   )IOLoop)r   r   future_add_done_callbackZtornado.ioloopr;   current
add_future)r1   r2   r:   r;   r   )r1   r2   r   r'      s
    
r'   z%Union[futures.Future[_T], Future[_T]])r    valuer   c             C   s   |   s| | dS )zSet 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)	cancelledr8   )r    r?   r   r   r   r      s    
r   )r    excr   c             C   s&   |   s| | n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)r@   r7   r   error)r    rA   r   r   r   %future_set_exception_unless_cancelled   s    rC   )r    r   r   c             C   s&   |d dk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)

    r-   Nz,future_set_exc_info called with no exception)r   rC   )r    r   r   r   r   r      s    r   zfutures.Future[_T])r    callbackr   c             C   s   d S )Nr   )r    rD   r   r   r   r<      s    r<   c             C   s   d S )Nr   )r    rD   r   r   r   r<      s    ).Nc             C   s    |   r||  n
| | 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)r4   add_done_callback)r    rD   r   r   r   r<      s    
)&__doc__asyncio
concurrentr   r*   r   typestornado.logr   typingr   r   r   r   r   TypeVarr	   r   r
   r   r   r$   r   Executorr   Zdummy_executorr0   objectZ
_NO_RESULTr'   r   BaseExceptionrC   typeTracebackTyper   overloadr<   r   r   r   r   <module>   sD   

@"