a
    ӗah                  
   @   s   d Z dZddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 dd	ej
ejg ejf  eje ejejejeje ejeje  f d
ddZdS )zFSupport for running coroutines in parallel with staggered start times.)staggered_race    N   )events)
exceptions)locks)tasks)loop)coro_fnsdelayr   returnc          	   	      s  p
t  t| ddg g tjtj dd fddd}| zd}|t	krt
I dH \}}t	|}|D ]$}| r| s| r| qqlfW D ]}|  qS ]}|  qnD ]}|  q0 dS )a  Run coroutines with staggered start times and take the first to finish.

    This method takes an iterable of coroutine functions. The first one is
    started immediately. From then on, whenever the immediately preceding one
    fails (raises an exception), or when *delay* seconds has passed, the next
    coroutine is started. This continues until one of the coroutines complete
    successfully, in which case all others are cancelled, or until all
    coroutines fail.

    The coroutines provided should be well-behaved in the following way:

    * They should only ``return`` if completed successfully.

    * They should always raise an exception if they did not complete
      successfully. In particular, if they handle cancellation, they should
      probably reraise, like this::

        try:
            # do work
        except asyncio.CancelledError:
            # undo partially completed work
            raise

    Args:
        coro_fns: an iterable of coroutine functions, i.e. callables that
            return a coroutine object when called. Use ``functools.partial`` or
            lambdas to pass arguments.

        delay: amount of time, in seconds, between starting coroutines. If
            ``None``, the coroutines will run sequentially.

        loop: the event loop to use.

    Returns:
        tuple *(winner_result, winner_index, exceptions)* where

        - *winner_result*: the result of the winning coroutine, or ``None``
          if no coroutines won.

        - *winner_index*: the index of the winning coroutine in
          ``coro_fns``, or ``None`` if no coroutines won. If the winning
          coroutine may return None on success, *winner_index* can be used
          to definitively determine whether any coroutine won.

        - *exceptions*: list of exceptions returned by the coroutines.
          ``len(exceptions)`` is equal to the number of coroutines actually
          started, and the order is the same as in ``coro_fns``. The winning
          coroutine's entry is ``None``.

    N)previous_failedr   c           	   
      s`  | d urJt tj& t|   I d H  W d    n1 s@0    Y  zt\}}W n tyn   Y d S 0 t	
 }|}| t|d ksJ d  t|d ksJ z| I d H }W nL ttfy    Y nr ty } z||< |  W Y d }~nFd }~0 0 d u s.J ||tD ]\}}||kr>|  q>d S )N   r   )
contextlibsuppressexceptions_modTimeoutErrorr   wait_forwaitnextStopIterationr   Eventcreate_taskappendlen
SystemExitKeyboardInterruptBaseExceptionset	enumeratecancel)	r   Z
this_indexZcoro_fnZthis_failedZ	next_taskresulteitr
   Zenum_coro_fnsr   r   run_one_coroZrunning_tasksZwinner_indexZwinner_result 4/home/manager/Python-3.9.10/Lib/asyncio/staggered.pyr%   R   s4    4


z$staggered_race.<locals>.run_one_coror   )r   get_running_loopr   typingOptionalr   r   r   r   r   r   r   done	cancelled	exceptionr   )	r	   r
   r   Z
first_taskZ
done_countr+   _dr#   r&   r$   r'   r      s4    =0


r   )__doc____all__r   r)    r   r   r   r   r   IterableCallable	Awaitabler*   floatAbstractEventLoopTupleAnyintList	Exceptionr   r&   r&   r&   r'   <module>   s&   