
    dafh                     f   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	d
ej                  ej                  g ej                  f      dej                  e   dej                  dej                   ej"                  ej                  e   ej&                  ej                  e      f   fdZy)zFSupport for running coroutines in parallel with staggered start times.)staggered_race    N   )events)
exceptions)locks)tasks)loopcoro_fnsdelayr	   returnc          	        	
K   xs t        j                         t        |       	ddg 
g dt        j                  t
        j                     ddf	
fdj                   d            }j                  |       	 d}|t              k7  rt        j                         d{   \  }}t        |      }	 |D ]D  }|j                         s|j                         r%|j                         s6|j                          |t              k7  r
fD ]  }|j                           S 7 # D ]  }|j                           w xY ww)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                   K   | Xt        j                  t        j                        5  t	        j
                  | j                         	       d {    d d d        	 t        
      \  }}t        j                         }j                   |            }j                  |       t              |dz   k(  sJ j                  d        t              |dz   k(  sJ 	  |        d {   }J ||t              D ]  \  }}||k7  s|j                           y 7 # 1 sw Y   xY w# t        $ r Y y w xY w7 S# t         t"        f$ r  t$        $ r}||<   |j'                          Y d }~y d }~ww xY ww)N   r   )
contextlibsuppressexceptions_modTimeoutErrorr   wait_forwaitnextStopIterationr   Eventcreate_taskappendlen	enumeratecancel
SystemExitKeyboardInterruptBaseExceptionset)r   
this_indexcoro_fnthis_failed	next_taskresultiter   enum_coro_fnsr   r	   run_one_cororunning_taskswinner_indexwinner_results            ,/root/Python-3.12.4/Lib/asyncio/staggered.pyr,   z$staggered_race.<locals>.run_one_coroR   s     &$$^%@%@A D
 nn_%9%9%;UCCCD	"&}"5J kkm$$\+%>?	Y'=!Z!^333$:*q.000	"9_F  '''%L"M "-0 1
?HHJG DD D  		 %-. 	 	%&Jz"OO	s   &E;(D$D"D$E;D0 -A3E;!
E +D?,E 0E;E;"D$$D-)E;0	D<9E;;D<<E;?E E8E3.E;3E88E;r   )r   get_running_loopr   typingOptionalr   r   r   r   r   r   r   done	cancelled	exceptionr   )r
   r   r	   
first_task
done_countr4   _dr)   r+   r   r,   r-   r.   r/   s    ``      @@@@@@r0   r   r      sM    z ,6**,Dh'MMLJM.#__U[[9.>B. .` !!,t"45J$ 
C..!JJ}55GD!TJ  ,Avvx!++-kkm+, C.. lJ6  	AHHJ	 6  	AHHJ	sH   BE(E 7D?8(E !E 2E E #E 'E?E EE)__doc____all__r   r2    r   r   r   r   r   IterableCallable	Awaitabler3   floatAbstractEventLoopTupleAnyintList	Exceptionr        r0   <module>rJ      s    L
    *   *.	G//&//"f6F6F2F"GHGu%G &&	G
 \\
JJ
OOC
KK	*+,GrI   