U
    ad\,                     @   s   d Z ddlZddlmZ ddlmZmZ ddlmZ zddl	m
Z
 W n ek
r\   dZ
Y nX ddd	d
ddgZzddl	mZ W n$ ek
r   G dd deZY nX G dd deZG dd	 d	ZG dd
 d
eZG dd deZG dd dZe
dkreZ
dS )z'A multi-producer, multi-consumer queue.    N)deque)heappushheappop)	monotonic)SimpleQueueEmptyFullQueuePriorityQueue	LifoQueuer   )r   c                   @   s   e Zd ZdZdS )r   z4Exception raised by Queue.get(block=0)/get_nowait().N__name__
__module____qualname____doc__ r   r   /usr/lib/python3.8/queue.pyr      s   c                   @   s   e Zd ZdZdS )r   z4Exception raised by Queue.put(block=0)/put_nowait().Nr   r   r   r   r   r      s   c                   @   s   e Zd ZdZd!ddZdd Zdd Zd	d
 Zdd Zdd Z	d"ddZ
d#ddZdd Zdd Zdd Zdd Zdd Zdd  ZdS )$r	   zjCreate a queue object with a given maximum size.

    If maxsize is <= 0, the queue size is infinite.
    r   c                 C   sN   || _ | | t | _t| j| _t| j| _t| j| _d| _	d S Nr   )
maxsize_init	threadingZLockmutexZ	Condition	not_emptynot_fullall_tasks_doneunfinished_tasksselfr   r   r   r   __init__!   s    

zQueue.__init__c              	   C   sH   | j 8 | jd }|dkr4|dk r*td| j   || _W 5 Q R X dS )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.
           r   z!task_done() called too many timesN)r   r   
ValueErrorZ
notify_all)r   Z
unfinishedr   r   r   	task_done8   s    

zQueue.task_donec              	   C   s(   | j  | jr| j   qW 5 Q R X dS )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   r   r   r   joinN   s    	z
Queue.joinc              
   C   s&   | j  |  W  5 Q R  S Q R X dS )9Return the approximate size of the queue (not reliable!).Nr   _qsizer#   r   r   r   qsize[   s    zQueue.qsizec              
   C   s(   | j  |   W  5 Q R  S Q R X dS )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&   r#   r   r   r   empty`   s    zQueue.emptyc              
   C   s<   | j , d| j  k o |  kn  W  5 Q R  S Q R X dS )aO  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'   r#   r   r   r   fulln   s    z
Queue.fullTNc              	   C   s   | j  | jdkr|s*|  | jkrtnr|dkrN|  | jkr| j   q2nN|dk r`tdn<t | }|  | jkr|t  }|dkrt| j | qj| | |  jd7  _| j	
  W 5 Q R X dS )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).
        r   N''timeout' must be a non-negative number        r   )r   r   r'   r   r"   r    time_putr   r   notify)r   itemblocktimeoutendtime	remainingr   r   r   puty   s&    




z	Queue.putc              
   C   s   | j  |s|  stnf|dkr8|  s| j   q"nH|dk rJtdn6t | }|  s|t  }|dkrrt| j | qT|  }| j  |W  5 Q R  S Q R X dS )  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).
        Nr   r+   r,   )	r   r'   r   r"   r    r-   _getr   r/   )r   r1   r2   r3   r4   r0   r   r   r   get   s$    



z	Queue.getc                 C   s   | j |ddS )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r1   r5   r   r0   r   r   r   
put_nowait   s    zQueue.put_nowaitc                 C   s   | j ddS zRemove and return an item from the queue without blocking.

        Only get an item if one is immediately available. Otherwise
        raise the Empty exception.
        Fr9   r8   r#   r   r   r   
get_nowait   s    zQueue.get_nowaitc                 C   s   t  | _d S N)r   queuer   r   r   r   r      s    zQueue._initc                 C   s
   t | jS r@   lenrA   r#   r   r   r   r'      s    zQueue._qsizec                 C   s   | j | d S r@   rA   appendr;   r   r   r   r.      s    z
Queue._putc                 C   s
   | j  S r@   )rA   popleftr#   r   r   r   r7      s    z
Queue._get)r   )TN)TN)r   r   r   r   r   r!   r$   r(   r)   r*   r5   r8   r<   r?   r   r'   r.   r7   r   r   r   r   r	      s   

 
c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r
   zVariant of Queue that retrieves open entries in priority order (lowest first).

    Entries are typically tuples of the form:  (priority number, data).
    c                 C   s
   g | _ d S r@   rA   r   r   r   r   r      s    zPriorityQueue._initc                 C   s
   t | jS r@   rB   r#   r   r   r   r'      s    zPriorityQueue._qsizec                 C   s   t | j| d S r@   )r   rA   r;   r   r   r   r.      s    zPriorityQueue._putc                 C   s
   t | jS r@   )r   rA   r#   r   r   r   r7      s    zPriorityQueue._getNr   r   r   r   r   r'   r.   r7   r   r   r   r   r
      s
   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   zBVariant of Queue that retrieves most recently added entries first.c                 C   s
   g | _ d S r@   rG   r   r   r   r   r      s    zLifoQueue._initc                 C   s
   t | jS r@   rB   r#   r   r   r   r'      s    zLifoQueue._qsizec                 C   s   | j | d S r@   rD   r;   r   r   r   r.      s    zLifoQueue._putc                 C   s
   | j  S r@   )rA   popr#   r   r   r   r7      s    zLifoQueue._getNrH   r   r   r   r   r      s
   c                   @   sL   e Zd ZdZdd ZdddZddd	Zd
d Zdd Zdd Z	dd Z
dS )_PySimpleQueuezYSimple, unbounded FIFO queue.

    This pure Python implementation is not reentrant.
    c                 C   s   t  | _td| _d S r   )r   _queuer   Z	Semaphore_countr#   r   r   r   r   	  s    z_PySimpleQueue.__init__TNc                 C   s   | j | | j  dS )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)rK   rE   rL   release)r   r0   r1   r2   r   r   r   r5     s    z_PySimpleQueue.putc                 C   s4   |dk	r|dk rt d| j||s*t| j S )r6   Nr   r+   )r    rL   acquirer   rK   rF   )r   r1   r2   r   r   r   r8     s
    z_PySimpleQueue.getc                 C   s   | j |ddS )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.
        Fr9   r:   r;   r   r   r   r<   '  s    z_PySimpleQueue.put_nowaitc                 C   s   | j ddS r=   r>   r#   r   r   r   r?   /  s    z_PySimpleQueue.get_nowaitc                 C   s   t | jdkS )zCReturn True if the queue is empty, False otherwise (not reliable!).r   rC   rK   r#   r   r   r   r)   7  s    z_PySimpleQueue.emptyc                 C   s
   t | jS )r%   rO   r#   r   r   r   r(   ;  s    z_PySimpleQueue.qsize)TN)TN)r   r   r   r   r   r5   r8   r<   r?   r)   r(   r   r   r   r   rJ      s   	
	
rJ   )r   r   collectionsr   heapqr   r   r-   r   rK   r   ImportError__all__r   	Exceptionr   r	   r
   r   rJ   r   r   r   r   <module>   s*   
 BA