Wc,cs.dZddlZddlZddlmZddlmZmZddlm Z ddl m Z gdZ ddl mZGd d eZGd dZGddeZGddeZGddZe eZ dSdS#e $rdZ YVwxYw#e $rGd d eZYbwxYw)z'A multi-producer, multi-consumer queue.N)deque)heappushheappop) monotonic) SimpleQueue)EmptyFullQueue PriorityQueue LifoQueuer)rceZdZdZdS)rz4Exception raised by Queue.get(block=0)/get_nowait().N__name__ __module__ __qualname____doc__/usr/lib64/python3.12/queue.pyrrs>> rrcr )rz4Exception raised by Queue.put(block=0)/put_nowait().Nr rrrrrs::DrrcseZdZdZddZdZdZdZdZdZ dd Z dd Z d Z dZ dZdZdZdZeej(Zd S)rzjCreate a queue object with a given maximum size. If maxsize is <= 0, the queue size is infinite. rcs`||_|j|tj|_tj |j|_tj |j|_tj |j|_d|_ dSNr) maxsize_init threadingZLockmutexZ Condition not_emptynot_fullall_tasks_doneunfinished_tasksselfr r__init__zQueue.__init__"s  7 ^^% #,,TZZ8"++DJJ7 (11$**= !rcs|j5|jdz }|dkr.|dkr td|jj||_ddddS#1swYdSxYw)a.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). Raises a ValueError if called more times than there were items placed in the queue. rz!task_done() called too many timesN)rr ValueErrorZ notify_all)rZ unfinishedrr task_donezQueue.task_done9s  / /..2JQ>>$%HII##..00$.D !  / / / / / / / / / / / / / / / / /s A A##A-cs|j5|jr(|jj|jr(ddddS#1swYdSxYw)aBlocks 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)rrwaitr rjoinz Queue.joinOs  + +'''##((**''' + + + + + + + + + + + + + + + + +s 4A  Acsl|j5|jcdddS#1swYdSxYw)9Return the approximate size of the queue (not reliable!).Nr_qsizer%r&rqsizez Queue.qsize\st ZZ ! !;;= ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !s)3csn|j5|j cdddS#1swYdSxYw)aReturn 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)r%r&remptyz Queue.emptyasyZZ % %{{}$ % % % % % % % % % % % % % % % % %s*4cs|j5d|jcxko|jknccdddS#1swYdSxYw)aOReturn 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. rN)rrr*r%r&rfullz Queue.fullosZZ 5 5t||4444t{{}4444 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s )AA TNcs.|j5|jdkr|s&|j|jkrt|[|j|jkr|jj |j|jkr;n|dkr t dt |z}|j|jkrX|t z }|dkrt|jj ||j|jkrX|j||xjdz c_|jjddddS#1swYdSxYw)aPut 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). rN''timeout' must be a non-negative numberr!) rrr*rr$r"time_putrrnotify)ritemblocktimeoutendtime remaining rputz Queue.putzs]] $ $||au{{} 44" _++-4<<77 **,,++-4<<777q[[$%NOO"fw.G++-4<<77$+df$4 $++"&J **955 ++-4<<77 IIdOO  ! ! !Q & ! ! NN ! ! # #' $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $sBF #BF =AF  FcsV|j5|s|jst|?|js|jj|js-n}|dkr t dt |z}|jsJ|t z }|dkrt|jj||jsJ|j }|jj|cdddS#1swYdSxYw)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). Nrr.r/) rr*rr$r"r0_getrr2)rr4r5r6r7r3r8rgetz Queue.getsC^^  5{{}}K++--NN''))++---1 !JKK&7*++-- '$& 0I C''# NN'' 22 ++-- 99;D MM " "%                 sAD(A=D&-DD(c*|j|dS)zPut an item into the queue without blocking. Only enqueue the item if a free slot is immediately available. Otherwise raise the Full exception. Fr4r9rr3rr put_nowaitzQueue.put_nowait xxEx**rc(|jdSzRemove and return an item from the queue without blocking. Only get an item if one is immediately available. Otherwise raise the Empty exception. Fr>r<r%r&r get_nowaitzQueue.get_nowait xxex$$rcs(t|_dSN)rqueuerrrrz Queue._initsW rc0t|jSrHlenrIr%r&rr*z Queue._qsize4::rc>|jj|dSrHrIappendr@rrr1z Queue._put $rc8|jjSrH)rIpopleftr%r&rr;z Queue._getszz!!##r)rTN)r rrrr r#r'r+r,r-r9r<rArFrr*r1r; classmethodtypes GenericAlias__class_getitem__rrrrrs """".///, + + +!!! % % % 5 5 5$$$$@>+++%%%   $$$$ E$6$67rrc*eZdZdZdZdZdZdZdS)r zVariant of Queue that retrieves open entries in priority order (lowest first). Entries are typically tuples of the form: (priority number, data). cg|_dSrHrIrrrrzPriorityQueue._init  rcrJrHrKr%r&rr*zPriorityQueue._qsizerMrcs6t|j|dSrH)rrIr@rrr1zPriorityQueue._putsT""""rcrJrH)rrIr%r&rr;zPriorityQueue._getstzz""rNr rrrrr*r1r;rrrr r sZ ########rr crY)r zBVariant of Queue that retrieves most recently added entries first.crZrHr[rrrrzLifoQueue._initr\rcrJrHrKr%r&rr*zLifoQueue._qsizerMrcrNrHrOr@rrr1zLifoQueue._putrQrcrRrH)rIZpopr%r&rr;zLifoQueue._getszz~~rNr]rrrr r sVLL        rr csfeZdZdZdZd dZd dZdZdZd Z d Z e e jZdS) _PySimpleQueuezYSimple, unbounded FIFO queue. This pure Python implementation is not reentrant. cs`t|_tjd|_dSr)r_queuerZ Semaphore_countr%r&rr z_PySimpleQueue.__init__ s!g ))!, rTNcst|jj||jjdS)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_rPr`Zrelease)rr3r4r5s rr9z_PySimpleQueue.puts2 4   rcs||dkr td|jj||st|jj S)r:Nrr.)r"r`Zacquirerr_rS)rr4r5s rr<z_PySimpleQueue.getsM  7Q;;FG G{{""5'22K{{""$$rcr=)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. Fr>r?r@rrrAz_PySimpleQueue.put_nowait*rBrcrCrDrEr%r&rrFz_PySimpleQueue.get_nowait2rGrcs8t|jdkS)zCReturn True if the queue is empty, False otherwise (not reliable!).rrLr_r%r&rr,z_PySimpleQueue.empty:s4;;1$$rcrJ)r(rar%r&rr+z_PySimpleQueue.qsize>s4;;rrT)r rrrr r9r<rArFr,r+rUrVrWrXrrrr^r^s---%%%%"+++%%%%%%   $ E$6$67rr^)rrrVZ collectionsrZheapqrrr0rr_rZ ImportErrorZ__all__rZ Exceptionrrr r r^rrrrbs-- ########"""""""""""" R Q Q       9  @8@8@8@8@8@8@8F#####E##&         @8@8@8@8@8@8@8F KKKw KKK             s"A3B3A=<A=BB