Wc,.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>> rrceZdZdZdS)r z4Exception raised by Queue.put(block=0)/put_nowait().Nrrrrr r s::Drr ceZdZdZddZdZdZdZdZdZ dd Z dd Z d Z dZ dZdZdZdZeej(Zd S)r zjCreate a queue object with a given maximum size. If maxsize is <= 0, the queue size is infinite. rc`||_|j|tj|_tj |j|_tj |j|_tj |j|_d|_ dSNr) maxsize_init threadingLockmutex Condition not_emptynot_fullall_tasks_doneunfinished_tasksselfrs r__init__zQueue.__init__"s  7 ^^% #,,TZZ8"++DJJ7 (11$**= !rc|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)r"r# ValueError notify_all)r% unfinisheds r task_donezQueue.task_done9s  / /..2JQ>>$%HII##..00$.D !  / / / / / / / / / / / / / / / / /s A A##A-c|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)r"r#waitr%s rjoinz Queue.joinOs  + +'''##((**''' + + + + + + + + + + + + + + + + +s 4A  Acl|j5|jcdddS#1swYdSxYw)9Return the approximate size of the queue (not reliable!).Nr_qsizer/s rqsizez Queue.qsize\st ZZ ! !;;= ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !s)3cn|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. Nr3r/s remptyz Queue.emptyasyZZ % %{{}$ % % % % % % % % % % % % % % % % %s*4c|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)rrr4r/s 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 TNc.|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() r!rr4r r.r)time_putr#r notify)r%itemblocktimeoutendtime remainings 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  FcV|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<) r r4rr.r)r=_getr!r?)r%rArBrCrDr@s rgetz 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. FrArEr%r@s r 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. FrKrIr/s r get_nowaitzQueue.get_nowait xxex$$rc(t|_dSN)rqueuer$s rrz Queue._initsW rc0t|jSrVlenrWr/s rr4z Queue._qsize4::rc>|jj|dSrVrWappendrMs rr>z Queue._put $rc8|jjSrV)rWpopleftr/s rrHz Queue._getszz!!##r)rTN)rrrrr&r,r0r5r7r9rErIrNrSrr4r>rH classmethodtypes GenericAlias__class_getitem__rrrr r s """".///, + + +!!! % % % 5 5 5$$$$@>+++%%%   $$$$ E$6$67rr c*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|_dSrVrWr$s rrzPriorityQueue._init  rc0t|jSrVrYr/s rr4zPriorityQueue._qsizer[rc6t|j|dSrV)rrWrMs rr>zPriorityQueue._putsT""""rc0t|jSrV)rrWr/s rrHzPriorityQueue._getstzz""rNrrrrrr4r>rHrrrr r sZ ########rr c*eZdZdZdZdZdZdZdS)r zBVariant of Queue that retrieves most recently added entries first.cg|_dSrVrir$s rrzLifoQueue._initrjrc0t|jSrVrYr/s rr4zLifoQueue._qsizer[rc>|jj|dSrVr]rMs rr>zLifoQueue._putr_rc8|jjSrV)rWpopr/s rrHzLifoQueue._getszz~~rNrnrrrr r sVLL        rr cfeZdZdZdZd dZd dZdZdZd Z d Z e e jZdS) _PySimpleQueuezYSimple, unbounded FIFO queue. This pure Python implementation is not reentrant. c`t|_tjd|_dSr)r_queuer Semaphore_countr/s rr&z_PySimpleQueue.__init__ s!g ))!, rTNct|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)rxr^rzrelease)r%r@rArBs rrEz_PySimpleQueue.puts2 4   rc||dkr td|jj||st|jj S)rGNrr;)r)rzacquirerrxra)r%rArBs rrIz_PySimpleQueue.getsM  7Q;;FG G{{""5'22K{{""$$rc*|j|dS)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. FrKrLrMs rrNz_PySimpleQueue.put_nowait*rOrc(|jdSrQrRr/s rrSz_PySimpleQueue.get_nowait2rTrc8t|jdkS)zCReturn True if the queue is empty, False otherwise (not reliable!).rrZrxr/s rr7z_PySimpleQueue.empty:s4;;1$$rc0t|jS)r2rr/s rr5z_PySimpleQueue.qsize>s4;;rrb)rrrrr&rErIrNrSr7r5rcrdrerfrrrrvrvs---%%%%"+++%%%%%%   $ E$6$67rrv)rrrd collectionsrheapqrrr=rrxr ImportError__all__r Exceptionr r r r rvrrrrs-- ########"""""""""""" 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