
    F\h4                     :   S r SSKrSSKrSSKJr  SSKJrJr  SSKJ	r   SSK
Jr  / SQr SSK
Jr   " S S\5      r " S S\5      r " S S5      r " S S\5      r " S S\5      r " S S5      r\c  \rgg! \ a    Sr N\f = f! \ a     " S	 S
\5      r Nhf = f)z'A multi-producer, multi-consumer queue.    N)deque)heappushheappop)	monotonic)SimpleQueue)EmptyFullShutDownQueuePriorityQueue	LifoQueuer   )r   c                       \ rS rSrSrSrg)r      z4Exception raised by Queue.get(block=0)/get_nowait(). N__name__
__module____qualname____firstlineno____doc____static_attributes__r       /usr/lib/python3.13/queue.pyr   r      s    >r   r   c                       \ rS rSrSrSrg)r	      z4Exception raised by Queue.put(block=0)/put_nowait().r   Nr   r   r   r   r	   r	      s    :r   r	   c                       \ rS rSrSrSrg)r
   $   z)Raised when put/get with shut-down queue.r   Nr   r   r   r   r
   r
   $   s    3r   r
   c                       \ rS rSrSrSS jrS rS rS rS r	S r
SS
 jrSS jrS rS rSS jrS rS rS rS r\" \R,                  5      rSrg	)r   (   zbCreate a queue object with a given maximum size.

If maxsize is <= 0, the queue size is infinite.
c                 `   Xl         U R                  U5        [        R                  " 5       U l        [        R
                  " U R                  5      U l        [        R
                  " U R                  5      U l        [        R
                  " U R                  5      U l        SU l	        SU l
        g )Nr   F)maxsize_init	threadingLockmutex	Condition	not_emptynot_fullall_tasks_doneunfinished_tasksis_shutdownselfr!   s     r   __init__Queue.__init__.   s~    

7 ^^%
 #,,TZZ8 "++DJJ7 (11$**= ! !r   c                     U R                      U R                  S-
  nUS::  a+  US:  a  [        S5      eU R                   R                  5         Xl        SSS5        g! , (       d  f       g= f)a8  Indicate that a formerly enqueued task is complete.

Used by Queue consumer threads.  For each get() used to fetch a task,
a subsequent call to task_done() tells the queue that the processing
on the task is complete.

If a join() is currently blocking, it will resume when all items
have been processed (meaning that a task_done() call was received
for every item that had been put() into the queue).

shutdown(immediate=True) calls task_done() for each remaining item in
the queue.

Raises a ValueError if called more times than there were items
placed in the queue.
   r   z!task_done() called too many timesN)r)   r*   
ValueError
notify_all)r-   
unfinisheds     r   	task_doneQueue.task_doneH   s[    "   ..2JQ>$%HII##..0$.! !  s   AA
A+c                     U R                      U R                  (       a-  U R                   R                  5         U R                  (       a  M-  SSS5        g! , (       d  f       g= f)a[  Blocks until all items in the Queue have been gotten and processed.

The count of unfinished tasks goes up whenever an item is added to the
queue. The count goes down whenever a consumer thread calls task_done()
to indicate the item was retrieved and all work on it is complete.

When the count of unfinished tasks drops to zero, join() unblocks.
N)r)   r*   waitr-   s    r   join
Queue.joina   sB       ''##((* ''' !  s   =A
A#c                 p    U R                      U R                  5       sSSS5        $ ! , (       d  f       g= f)9Return the approximate size of the queue (not reliable!).Nr%   _qsizer9   s    r   qsizeQueue.qsizen   s    ZZ;;= ZZs   '
5c                 z    U R                      U R                  5       (       + sSSS5        $ ! , (       d  f       g= f)a  Return True if the queue is empty, False otherwise (not reliable!).

This method is likely to be removed at some point.  Use qsize() == 0
as a direct substitute, but be aware that either approach risks a race
condition where a queue can grow before the result of empty() or
qsize() can be used.

To create code that needs to wait for all queued tasks to be
completed, the preferred technique is to use the join() method.
Nr>   r9   s    r   emptyQueue.emptys   s!     ZZ{{}$ ZZs   ,
:c                     U R                      SU R                  s=:  =(       a    U R                  5       :*  Os  sSSS5        $ ! , (       d  f       g= f)a'  Return True if the queue is full, False otherwise (not reliable!).

This method is likely to be removed at some point.  Use qsize() >= n
as a direct substitute, but be aware that either approach risks a race
condition where a queue can shrink before the result of full() or
qsize() can be used.
r   N)r%   r!   r?   r9   s    r   full
Queue.full   s/     ZZt||44t{{}4 ZZs   -A
ANc                    U R                      U R                  (       a  [        eU R                  S:  GaG  U(       d&  U R	                  5       U R                  :  a  [
        eGOUcp  U R	                  5       U R                  :  aQ  U R                   R                  5         U R                  (       a  [        eU R	                  5       U R                  :  a  MQ  OUS:  a  [        S5      e[        5       U-   nU R	                  5       U R                  :  ak  U[        5       -
  nUS::  a  [
        eU R                   R                  U5        U R                  (       a  [        eU R	                  5       U R                  :  a  Mk  U R                  U5        U =R                  S-  sl
        U R                  R                  5         SSS5        g! , (       d  f       g= f)a  Put an item into the queue.

If optional args 'block' is true and 'timeout' is None (the default),
block if necessary until a free slot is available. If 'timeout' is
a non-negative number, it blocks at most 'timeout' seconds and raises
the Full exception if no free slot was available within that time.
Otherwise ('block' is false), put an item on the queue if a free slot
is immediately available, else raise the Full exception ('timeout'
is ignored in that case).

Raises ShutDown if the queue has been shut down.
r   N''timeout' must be a non-negative number        r1   )r(   r+   r
   r!   r?   r	   r8   r2   time_putr*   r'   notify)r-   itemblocktimeoutendtime	remainings         r   put	Queue.put   sF    ]]||a{{}4"
 5_++-4<<7**,++"*N ++-4<<7 q[$%NOO"fw.G++-4<<7$+df$4	$+"&J**95++"*N ++-4<<7 IIdO!!Q&!NN!!#3 ]]s   CGB&G=A G
Gc                    U R                      U R                  (       a  U R                  5       (       d  [        eU(       d  U R                  5       (       d  [        eGO Ucs  U R                  5       (       d]  U R                   R                  5         U R                  (       a  U R                  5       (       d  [        eU R                  5       (       d  M]  OUS:  a  [        S5      e[        5       U-   nU R                  5       (       dw  U[        5       -
  nUS::  a  [        eU R                   R                  U5        U R                  (       a  U R                  5       (       d  [        eU R                  5       (       d  Mw  U R                  5       nU R                  R                  5         UsSSS5        $ ! , (       d  f       g= f)a6  Remove and return an item from the queue.

If optional args 'block' is true and 'timeout' is None (the default),
block if necessary until an item is available. If 'timeout' is
a non-negative number, it blocks at most 'timeout' seconds and raises
the Empty exception if no item was available within that time.
Otherwise ('block' is false), return an item if one is immediately
available, else raise the Empty exception ('timeout' is ignored
in that case).

Raises ShutDown if the queue has been shut down and is empty,
or if the queue has been shut down immediately.
Nr   rI   rJ   )r'   r+   r?   r
   r   r8   r2   rK   _getr(   rM   )r-   rO   rP   rQ   rR   rN   s         r   get	Queue.get   s'    ^^{{}}K %++--NN'')''& ++-- 1 !JKK&7*++-- '$& 0I C'#NN''	2''& ++-- 99;DMM  "1 ^^s   CF3B)F3>+F33
Gc                 "    U R                  USS9$ )zPut an item into the queue without blocking.

Only enqueue the item if a free slot is immediately available.
Otherwise raise the Full exception.
FrO   rS   r-   rN   s     r   
put_nowaitQueue.put_nowait        xxEx**r   c                      U R                  SS9$ zRemove and return an item from the queue without blocking.

Only get an item if one is immediately available. Otherwise
raise the Empty exception.
FrZ   rW   r9   s    r   
get_nowaitQueue.get_nowait        xxex$$r   c                    U R                      SU l        U(       a{  U R                  5       (       aL  U R                  5         U R                  S:  a  U =R                  S-  sl        U R                  5       (       a  ML  U R
                  R                  5         U R                  R                  5         U R                  R                  5         SSS5        g! , (       d  f       g= f)aj  Shut-down the queue, making queue gets and puts raise ShutDown.

By default, gets will only raise once the queue is empty. Set
'immediate' to True to make gets raise immediately instead.

All blocked callers of put() and get() will be unblocked. If
'immediate', a task is marked as done for each item remaining in
the queue, which may unblock callers of join().
Tr   r1   N)	r%   r+   r?   rV   r*   r)   r3   r'   r(   )r-   	immediates     r   shutdownQueue.shutdown   s     ZZ#DkkmmIIK,,q0--2- kkmm
 ##..0NN%%'MM$$& ZZs   A.C=AC
C"c                 "    [        5       U l        g N)r   queuer,   s     r   r"   Queue._init  s    W
r   c                 ,    [        U R                  5      $ rk   lenrl   r9   s    r   r?   Queue._qsize      4::r   c                 :    U R                   R                  U5        g rk   rl   appendr\   s     r   rL   
Queue._put      

$r   c                 6    U R                   R                  5       $ rk   )rl   popleftr9   s    r   rV   
Queue._get  s    zz!!##r   )r)   r+   r!   r%   r'   r(   rl   r*   )r   TN)F)r   r   r   r   r   r.   r5   r:   r@   rC   rF   rS   rW   r]   rc   rh   r"   r?   rL   rV   classmethodtypesGenericAlias__class_getitem__r   r   r   r   r   r   (   sg    
!4/2+!
%	5&$P&P+%'8 $ $E$6$67r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	r   i  zVariant of Queue that retrieves open entries in priority order (lowest first).

Entries are typically tuples of the form:  (priority number, data).
c                     / U l         g rk   rl   r,   s     r   r"   PriorityQueue._init  	    
r   c                 ,    [        U R                  5      $ rk   ro   r9   s    r   r?   PriorityQueue._qsize"  rr   r   c                 0    [        U R                  U5        g rk   )r   rl   r\   s     r   rL   PriorityQueue._put%  s    T"r   c                 ,    [        U R                  5      $ rk   )r   rl   r9   s    r   rV   PriorityQueue._get(  s    tzz""r   r   N
r   r   r   r   r   r"   r?   rL   rV   r   r   r   r   r   r     s    
##r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	r   i,  zBVariant of Queue that retrieves most recently added entries first.c                     / U l         g rk   r   r,   s     r   r"   LifoQueue._init/  r   r   c                 ,    [        U R                  5      $ rk   ro   r9   s    r   r?   LifoQueue._qsize2  rr   r   c                 :    U R                   R                  U5        g rk   rt   r\   s     r   rL   LifoQueue._put5  rw   r   c                 6    U R                   R                  5       $ rk   )rl   popr9   s    r   rV   LifoQueue._get8  s    zz~~r   r   Nr   r   r   r   r   r   ,  s    L  r   r   c                   n    \ rS rSrSrS rSS jrSS jrS rS r	S	 r
S
 r\" \R                  5      rSrg)_PySimpleQueuei<  zQSimple, unbounded FIFO queue.

This pure Python implementation is not reentrant.
c                 X    [        5       U l        [        R                  " S5      U l        g )Nr   )r   _queuer#   	Semaphore_countr9   s    r   r.   _PySimpleQueue.__init__F  s    g))!,r   Nc                 n    U R                   R                  U5        U R                  R                  5         g)zPut the item on the queue.

The optional 'block' and 'timeout' arguments are ignored, as this method
never blocks.  They are provided for compatibility with the Queue class.
N)r   ru   r   release)r-   rN   rO   rP   s       r   rS   _PySimpleQueue.putJ  s&     	4 r   c                     Ub  US:  a  [        S5      eU R                  R                  X5      (       d  [        eU R                  R                  5       $ )a  Remove and return an item from the queue.

If optional args 'block' is true and 'timeout' is None (the default),
block if necessary until an item is available. If 'timeout' is
a non-negative number, it blocks at most 'timeout' seconds and raises
the Empty exception if no item was available within that time.
Otherwise ('block' is false), return an item if one is immediately
available, else raise the Empty exception ('timeout' is ignored
in that case).
r   rI   )r2   r   acquirer   r   ry   )r-   rO   rP   s      r   rW   _PySimpleQueue.getS  sI     7Q;FGG{{""522K{{""$$r   c                 "    U R                  USS9$ )zPut an item into the queue without blocking.

This is exactly equivalent to `put(item, block=False)` and is only provided
for compatibility with the Queue class.
FrZ   r[   r\   s     r   r]   _PySimpleQueue.put_nowaitd  r_   r   c                      U R                  SS9$ ra   rb   r9   s    r   rc   _PySimpleQueue.get_nowaitl  re   r   c                 2    [        U R                  5      S:H  $ )zCReturn True if the queue is empty, False otherwise (not reliable!).r   rp   r   r9   s    r   rC   _PySimpleQueue.emptyt  s    4;;1$$r   c                 ,    [        U R                  5      $ )r=   r   r9   s    r   r@   _PySimpleQueue.qsizex  s    4;;r   )r   r   r{   )r   r   r   r   r   r.   rS   rW   r]   rc   rC   r@   r|   r}   r~   r   r   r   r   r   r   r   <  s=    -%"+%%  $E$6$67r   r   )r   r#   r}   collectionsr   heapqr   r   rK   r   r   r   ImportError__all__r   	Exceptionr	   r
   r   r   r   r   r   r   r   <module>r      s    -    # ""	9 	
4y 4n8 n8b#E #&    @8 @8F  K k  K   	 s"   A8 B 8BBBB