3 \L" @sdZy ddlZWnek r,ddlZYnXddlmZddlmZmZddl m Z dddd d gZ Gd dde Z Gd dde ZGd ddZGdd d eZGdd d eZdS)z'A multi-producer, multi-consumer queue.N)deque)heappushheappop) monotonicEmptyFullQueue PriorityQueue LifoQueuec@seZdZdZdS)rz4Exception raised by Queue.get(block=0)/get_nowait().N)__name__ __module__ __qualname____doc__rr/usr/lib64/python3.6/queue.pyr sc@seZdZdZdS)rz4Exception raised by Queue.put(block=0)/put_nowait().N)r r r rrrrrrsc@seZdZdZd!ddZddZddZd d Zd d Zd dZ d"ddZ d#ddZ ddZ ddZ ddZddZddZdd ZdS)$rzjCreate a queue object with a given maximum size. If maxsize is <= 0, the queue size is infinite. rcCsN||_|j|tj|_tj|j|_tj|j|_tj|j|_d|_ dS)Nr) maxsize_init threadingZLockmutexZ Condition not_emptynot_fullall_tasks_doneunfinished_tasks)selfrrrr__init__s  zQueue.__init__c CsH|j8|jd}|dkr4|dkr*td|jj||_WdQRXdS)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_done2s  zQueue.task_donec Cs,|jx|jr|jjq WWdQRXdS)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)rrwait)rrrrjoinHs z Queue.joinc Cs|j |jSQRXdS)z9Return the approximate size of the queue (not reliable!).N)r_qsize)rrrrqsizeUsz Queue.qsizec Cs|j |j SQRXdS)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. N)rr )rrrremptyZs z Queue.emptyc Cs0|j d|jko |jkSSQRXdS)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 )rrrrfullhsz Queue.fullTNc Cs|j|jdkr|s*|j|jkrtnz|dkrRxp|j|jkrN|jjq4WnR|dkrdtdn@t|}x4|j|jkr|t}|dkrt|jj|qpW|j||jd7_|j j WdQRXdS)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). rNz''timeout' must be a non-negative numbergr) rrr rrrtime_putrrnotify)ritemblocktimeoutendtime remainingrrrputss&      z Queue.putc Cs|j|s|jstnn|dkrs   B