
    F\h                     R    S r SrSSKrSSKJr  SSKJr  SSKJr  SSKJr  SS	.S
 jr	g)zFSupport for running coroutines in parallel with staggered start times.)staggered_race    N   )events)
exceptions)locks)tasks)loopc          
        ^^^^	^
^^^^^^#    T=(       d    [         R                  " 5       m[        U 5      mSmSm/ m/ m	[        5       mSm
U
UU4S jmSUUU	UUUUUU4	S jjmSn [        R
                  " 5       nTR                  T" US5      5      nTR                  U5        UR                  T5        UR                  5         SnT(       a&  TR                  5       m
 T
I Sh  vN   Sm
T(       a  M&  T(       a  [        ST5      eUb  UeTTT	4@	A@$  N/! [        R                   a.  nUnT H  nUR                  " UR                  6   M      SnANjSnAff = f! @	A@f = f7f)ai  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``.

Nc                    > TR                  U 5        Tb-  TR                  5       (       d  T(       d  TR                  S 5        U R                  5       (       a  g U R	                  5       nUc  g TR                  U5        g )N)discarddone
set_result	cancelled	exceptionappend)taskexcon_completed_futrunning_tasksunhandled_exceptionss     (/usr/lib/python3.13/asyncio/staggered.py	task_done!staggered_race.<locals>.task_doneJ   si    d#($))++!''->>nn;##C(    c                   >	#    U R                  5       I S h  vN   UbZ  [        R                  " [        R                  5         [
        R                  " UR                  5       T5      I S h  vN   S S S 5         [        T5      u  p#[        R                  " 5       n[        R                  " 5       nTR                  T" XT5      5      nTR                  U5        UR                  T5        UR                  5         TR                  S 5        [!        T5      US-   :X  d   e U" 5       I S h  vN nTb   eUmUm[
        R"                  " T5      nT H  n	XLd  M	  U	R%                  5         M     g  GNW GN! , (       d  f       GN
= f! [         a     g f = f Nk! [&        [(        4 a    e [*         a  n
U
TU'   UR                  5          S n
A
g S n
A
ff = f7f)Nr   )wait
contextlibsuppressexceptions_modTimeoutErrorr   wait_fornextStopIterationr   Eventcreate_taskaddadd_done_callbacksetr   lencurrent_taskcancel
SystemExitKeyboardInterruptBaseException)ok_to_startprevious_failed
this_indexcoro_fnthis_failednext_ok_to_start	next_taskresultr*   tedelayenum_coro_fnsr   r	   run_one_coror   r   winner_indexwinner_results              r   r;   $staggered_race.<locals>.run_one_coro[   s        &$$^%@%@A
 nn_%9%9%;UCCC B	"&}"5J kkm ;;=$$\2B%PQ	)$##I. 	$:*q.000	"9_F  '''%L"M !--d3L"(HHJ #a 	! D BA  		  %-. 	 	%&Jz"OO	s   GE.+G)E4,E1-E41G:F BG 
F *F+F /&GG1E44
F>G
FGFGF G2GGGGzstaggered race failed)returnN)r   get_running_loop	enumerater(   r   r$   r%   r&   r'   create_futurer   CancelledErrorr+   argsExceptionGroup)coro_fnsr9   r	   propagate_cancellation_errorr/   
first_taskexr   r:   r   r   r;   r   r   r   r<   r=   s    ``     @@@@@@@@@r   r   r      s_    h ,6**,Dh'MMLJEM)"6 6p $( Kkkm%%l;&EF
*%$$Y/'+$#113*&&&
  $ m . !!8:NOO'3..lJ646J '!00 */1,)DKK) ** 46Jsa   AE#&A8E D $D%D )	E 4E E#D E*$EE EE E  E#)
__doc____all__r    r   r   r   r   r   r    r   r   <module>rN      s(    L
   *   37 aKr   