a Z^,c@sdZddlZddlZddlmZddlmZmZddlm Zzddl m Z Wne ybdZ Yn0gdZ zddl mZWn"e yGd d d eZYn0Gd d d eZGd ddZGdddeZGdddeZGdddZe dureZ dS)z'A multi-producer, multi-consumer queue.N)deque)heappushheappop) monotonic) SimpleQueue)EmptyFullQueue PriorityQueue LifoQueuer)rc@eZdZdZdS)rz4Exception raised by Queue.get(block=0)/get_nowait().N__name__ __module__ __qualname____doc__rr/usr/lib64/python3.9/queue.pyrrc@r )rz4Exception raised by Queue.put(block=0)/put_nowait().Nr rrrrrrrc@seZdZdZd!ddZddZddZd d Zd d Zd dZ d"ddZ d#ddZ ddZ ddZ ddZddZddZdd ZeejZdS)$rzjCreate a queue object with a given maximum size. If maxsize is <= 0, the queue size is infinite. rcCsN||_||t|_t|j|_t|j|_t|j|_d|_ dSNr) maxsize_init threadingZLockmutexZ Condition not_emptynot_fullall_tasks_doneunfinished_tasksselfrrrr__init__"s  zQueue.__init__cCs\|jB|jd}|dkr4|dkr*td|j||_Wdn1sN0YdS)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 unfinishedrrr task_done9s  zQueue.task_donecCs<|j"|jr|jqWdn1s.0YdS)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)rrwaitrrrrjoinOs z Queue.joincCs2|j|WdS1s$0YdS)9Return the approximate size of the queue (not reliable!).Nr_qsizer$rrrqsize\sz Queue.qsizecCs4|j| WdS1s&0YdS)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$rrremptyas z Queue.emptycCsH|j.d|jko |knWdS1s:0YdS)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$rrrfullosz Queue.fullTNcCs|j|jdkr|s*||jkrtnr|durN||jkr|jq2nN|dkr`tdnrCrr(r/r8 classmethodtypes GenericAlias__class_getitem__rrrrrs     rc@0eZdZdZddZddZddZdd Zd S) r zVariant of Queue that retrieves open entries in priority order (lowest first). Entries are typically tuples of the form: (priority number, data). cC g|_dSrDrErrrrrrFzPriorityQueue._initcCrGrDrHr$rrrr(rFzPriorityQueue._qsizecCst|j|dSrD)rrEr=rrrr/rFzPriorityQueue._putcCrGrD)rrEr$rrrr8rFzPriorityQueue._getNr rrrrr(r/r8rrrrr s r c@rT) r zBVariant of Queue that retrieves most recently added entries first.cCrUrDrVrrrrrrFzLifoQueue._initcCrGrDrHr$rrrr(rFzLifoQueue._qsizecCrJrDrKr=rrrr/rFzLifoQueue._putcCrMrD)rEZpopr$rrrr8rFzLifoQueue._getNrWrrrrr s r c@sVeZdZdZddZdddZddd Zd d Zd d ZddZ ddZ e e j ZdS)_PySimpleQueuezYSimple, unbounded FIFO queue. This pure Python implementation is not reentrant. cCst|_td|_dSr)r_queuerZ Semaphore_countr$rrrr sz_PySimpleQueue.__init__TNcCs|j||jdS)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)rYrLrZZrelease)rr1r2r3rrrr6s z_PySimpleQueue.putcCs4|dur|dkrtd|j||s*t|jS)r7Nrr,)r!rZZacquirerrYrN)rr2r3rrrr9s z_PySimpleQueue.getcCr:)zPut an item into the queue without blocking. This is exactly equivalent to `put(item)` and is only provided for compatibility with the Queue class. Fr;r<r=rrrr>*r?z_PySimpleQueue.put_nowaitcCr@rArBr$rrrrC2r?z_PySimpleQueue.get_nowaitcCst|jdkS)zCReturn True if the queue is empty, False otherwise (not reliable!).rrIrYr$rrrr*:z_PySimpleQueue.emptycCrG)r&r[r$rrrr)>r\z_PySimpleQueue.qsizerOrO)r rrrrr6r9r>rCr*r)rPrQrRrSrrrrrXs  rX)rrrQZ collectionsrZheapqrrr.rrYrZ ImportErrorZ__all__rZ Exceptionrrr r rXrrrrZs,     DC