
    Dh               
       .   d Z ddlZddlZddlmZmZmZmZmZm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZmZmZmZ ddde
ee      d	ee   d
ee   fdZddde
ee      ded
ee   fdZddddee   deded
ee   fdZ	 	 ddeee      deded
ee   fdZy)zt
Friendlier version of asyncio standard library.

Provisional library.  Must be imported as `aioitertools.asyncio`.
    N)AnyAsyncGeneratorAsyncIterable	AwaitablecastDictIterableListOptionalSetTuple   )itermaybe_await)AnyIterableAsyncIteratorMaybeAwaitableT)timeoutawsr   returnc                 K   t               }| D ch c]  }t        j                  |       }}d}|r|dkD  rt        j                         |z   }nd}|r|rbt        j                         z
  }|dk  rF|D ]-  }t	        |t        j
                        r|j                          ./ t        j                         t        t        t        t        t              t        t        t              f   t        j                  ||t        j                         d{         \  }}|D ]  }| d{     |ryyc c}w 7 )7 w)a  
    Run awaitables in `aws` concurrently, and yield results as they complete.

    Unlike `asyncio.as_completed`, this yields actual results, and does not require
    awaiting each item in the iterable.

    Cancels all remaining awaitables if a timeout is given and the timeout threshold
    is reached.

    Example::

        async for value in as_completed(futures):
            ...  # use value immediately

    Nr   )r   return_when)setasyncioensure_futuretime
isinstanceFuturecancelTimeoutErrorr   r   r   r   r   waitFIRST_COMPLETED)	r   r   doneapending	remaining	thresholdfutitems	            v/var/www/fastuser/data/www/generator.snapmosaic.io/flask_app/venv/lib/python3.12/site-packages/aioitertools/asyncio.pyas_completedr,      s%    ( "eDDG!Hq'"7"7":!HG!H!%I7Q;IIK')	
!DIIK/IA~" C!#w~~6

	
 **,, #il#S1%667,,!#33 
g  	D*	7  "I8 s4   ED9C'ED>
E*E +E7E EF)return_exceptions	iterablesr-   c               
  	
K   t        j                         	t        j                         
dt        t           ddf	
fd}| D cg c]  }t        j                   ||             }}t        |      }	 |r5	 	j                         }|r| n|	 	 
j                         }| |r5|D ]#  }|j                         r|j                          % |D ]  }	 | d{     yc c}w # t         j                  $ r Y nw xY w# t         j                  $ rS t        |      D ]$  }|j                         s|j                  |       & t        j                  d       d{  7   Y w xY w# t         j                  t        f$ r Y w xY w7 # t         j                  $ r Y w xY w# |D ]#  }|j                         r|j                          % |D ]'  }	 | d{  7   # t         j                  $ r Y %w xY w w xY ww)ao  
    Yield results from one or more async iterables, in the order they are produced.

    Like :func:`as_completed`, but for async iterators or generators instead of futures.
    Creates a separate task to drain each iterable, and a single queue for results.

    If ``return_exceptions`` is ``False``, then any exception will be raised, and
    pending iterables and tasks will be cancelled, and async generators will be closed.
    If ``return_exceptions`` is ``True``, any exceptions will be yielded as results,
    and execution will continue until all iterables have been fully consumed.

    Example::

        async def generator(x):
            for i in range(x):
                yield i

        gen1 = generator(10)
        gen2 = generator(12)

        async for value in as_generated([gen1, gen2]):
            ...  # intermixed values yielded from gen1 and gen2
    r   r   Nc                 <  K   	 | 2 3 d {   }j                  |       d {    #7 7 6 y # t        j                  $ r+ t        | t              r| j                          d {  7    t        $ r$}j                  |       d {  7   Y d }~y d }~ww xY ww)N)putr   CancelledErrorr   r   aclose	Exception)r   r*   e	exc_queuequeues      r+   tailerzas_generated.<locals>.tailerz   s     	#" & &diio%%&% #%% 	$/kkm## 	#--"""	#sg   B0 .*.0 ,0 .0 0 B6B&A)'B5B	B
BBBBgMbP?)r   Queuer   r   r   r   
get_nowait
QueueEmptylistr$   removesleepr2   GeneratorExitr    )r.   r-   r8   r   tasksr&   excvaluetaskr6   r7   s            @@r+   as_generatedrD   Z   s    : +2--/I%mmoE	#=+ 	# 	# >GGTW""6$<0GEG%jG **,$II +((* .  	D99;	  	D

	? H %%  %% + M -Dyy{t,- mmE***	+ ""M2  ))   	D99;	  	D

)) 	s  AH"C))H7E0 :C. E0 D +E0 .HHF!F"F&H.DE0 DE0 1E-9+E-$E'%E-*E0 ,E--E0 0F	F* FF* FF'$H&F''H*H H G%G!
G%$H %G;	8H :G;	;H  Hr-   limitargsrG   c                   K   i }i }dgt        |      z  }t               }t               }d}	 |t        |      k  r|dk(  st        |      |k  r~||   |v r|||      j                  |       n7t        j                  ||         }	|j                  |	       |||	<   |g|||   <   |dz  }|t        |      k  r|dk(  rot        |      |k  r~|rx	 t        j                  |t        j                         d{   \  }}|D ]A  }
| r'|
j                         r|
j                         |||
   <   ,|
j                         |||
   <   C 	 |s|t        |      k(  rn,|j                         D ]*  }t        dt        |            D ]  }||d      |||   <    , |S 7 # t        j                  $ r7 |D ]  }
|
j                           t        j                  |ddi d{  7    w xY ww)aR  
    Like asyncio.gather but with a limit on concurrency.

    Note that all results are buffered.

    If gather is cancelled all tasks that were internally created and still pending
    will be cancelled as well.

    Example::

        futures = [some_coro(i) for i in range(10)]

        results = await gather(*futures, limit=2)
    Nr   TrE   r   )r   r-   )lenr   appendr   r   addr"   r#   	exceptionresultr2   r    gathervaluesrange)r-   rG   rH   	input_mapposretr&   r$   next_argrC   xlstis                r+   rO   rO      s    * 02I(*CVc$i'C&)eG#&5DH
T"s7|e7K H~*$x.)00: +2*?*?X*OD!$D	-5J	$x.)MH T"s7|e7K& &-ll)@)@' !g  1A(Q[[]&'kkmCF&'hhjCF	1 8s4y0O R ! &q#c(# 	&Ac!f+CAK	&& J/! ))   AHHJ nngFFFFsK   B:G'=G'G'(F 7F8AF AG'F AG$GG$$G'itrc                    K   t        t        |       2 cg c3 d{   }t        |      7 6 nc c}w c}||d d{  7  S w)z
    Wrapper around gather to handle gathering an iterable instead of ``*args``.

    Note that the iterable values don't have to be awaitable.
    NrF   )rO   aiterr   )rY   r-   rG   rX   s       r+   gather_iterr\      sE      (-c
	3	31+a.	3	3	3+   s2   A0.
,.
0.
0A AA)FrE   )__doc__r   r   typingr   r   r   r   r   r   r	   r
   r   r   r   builtinsr   r[   r   typesr   r   r   r   floatr,   boolrD   intrO   r\        r+   <module>rf      s"        1 @ @  $9	)A,	9 e_9 1	9~ $Na()N N 1	Nf $LQ<LL L 
#Y	Lb $	^A&	'  
!W	re   