î
\á[\;#  ã               @   sû   d  Z  d d d d d d g Z d d l Z d d l Z d	 d
 l m Z d	 d l m Z d	 d l m Z d	 d l m	 Z	 Gd d „  d e
 ƒ Z Gd d „  d e
 ƒ Z Gd d „  d ƒ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z d S)ZQueuesÚQueueÚPriorityQueueÚ	LifoQueueÚJoinableQueueÚ	QueueFullÚ
QueueEmptyé    Né   )Úevents)Úfutures)Úlocks)Ú	coroutinec               @   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.4/asyncio/queues.pyr      s   c               @   s   e  Z d  Z d Z d S)r   z4Exception raised by Queue.put(block=0)/put_nowait().N)r   r   r   r   r   r   r   r   r      s   c               @   sý   e  Z d  Z d Z d d d d d „Z d d „  Z d	 d
 „  Z d d „  Z d d „  Z d d „  Z	 d d „  Z
 d d „  Z d d „  Z d d „  Z e d d „  ƒ Z d d „  Z d d „  Z e d d  „  ƒ Z d! d" „  Z e d# d$ „  ƒ Z d% d& „  Z d S)'r   a  A queue, useful for coordinating producer and consumer coroutines.

    If maxsize is less than or equal to zero, the queue size is infinite. If it
    is an integer greater than 0, then "yield from put()" will block when the
    queue reaches maxsize, until an item is removed by get().

    Unlike the standard library Queue, you can reliably know this Queue's size
    with qsize(), since your single-threaded asyncio application won't be
    interrupted between calling qsize() and doing an operation on the Queue.
    r   ÚloopNc            C   s_   | d  k r t  j ƒ  |  _ n	 | |  _ | |  _ t j ƒ  |  _ t j ƒ  |  _ |  j | ƒ d  S)N)	r	   Zget_event_loopÚ_loopÚ_maxsizeÚcollectionsÚdequeÚ_gettersÚ_puttersÚ_init)ÚselfÚmaxsizer   r   r   r   Ú__init__%   s    		zQueue.__init__c             C   s   t  j ƒ  |  _ d  S)N)r   r   Ú_queue)r   r   r   r   r   r   2   s    zQueue._initc             C   s   |  j  j ƒ  S)N)r   Úpopleft)r   r   r   r   Ú_get5   s    z
Queue._getc             C   s   |  j  j | ƒ d  S)N)r   Úappend)r   Úitemr   r   r   Ú_put8   s    z
Queue._putc             C   s(   d j  t |  ƒ j t |  ƒ |  j ƒ  ƒ S)Nz<{} at {:#x} {}>)ÚformatÚtyper   ÚidÚ_format)r   r   r   r   Ú__repr__;   s    zQueue.__repr__c             C   s   d j  t |  ƒ j |  j ƒ  ƒ S)Nz<{} {}>)r$   r%   r   r'   )r   r   r   r   Ú__str__?   s    zQueue.__str__c             C   s—   d j  |  j ƒ } t |  d d  ƒ rC | d j  t |  j ƒ ƒ 7} n  |  j rk | d j  t |  j ƒ ƒ 7} n  |  j r“ | d j  t |  j ƒ ƒ 7} n  | S)Nzmaxsize={!r}r   z _queue={!r}z _getters[{}]z _putters[{}])r$   r   ÚgetattrÚlistr   r   Úlenr   )r   Úresultr   r   r   r'   B   s    		zQueue._formatc             C   s4   x- |  j  r/ |  j  d j ƒ  r/ |  j  j ƒ  q Wd  S)Nr   )r   Údoner   )r   r   r   r   Ú_consume_done_gettersL   s    zQueue._consume_done_gettersc             C   s8   x1 |  j  r3 |  j  d d j ƒ  r3 |  j  j ƒ  q Wd  S)Nr   r   )r   r.   r   )r   r   r   r   Ú_consume_done_puttersQ   s    #zQueue._consume_done_puttersc             C   s   t  |  j ƒ S)zNumber of items in the queue.)r,   r   )r   r   r   r   ÚqsizeV   s    zQueue.qsizec             C   s   |  j  S)z%Number of items allowed in the queue.)r   )r   r   r   r   r   Z   s    zQueue.maxsizec             C   s   |  j  S)z3Return True if the queue is empty, False otherwise.)r   )r   r   r   r   Úempty_   s    zQueue.emptyc             C   s*   |  j  d k r d S|  j ƒ  |  j  k Sd S)zªReturn True if there are maxsize items in the queue.

        Note: if the Queue was initialized with maxsize=0 (the default),
        then full() is never True.
        r   FN)r   r1   )r   r   r   r   Úfullc   s    z
Queue.fullc             c   sÇ   |  j  ƒ  |  j r[ |  j s) t d ƒ ‚ |  j j ƒ  } |  j | ƒ | j |  j ƒ  ƒ nh |  j d k r¶ |  j |  j	 ƒ  k r¶ t
 j d |  j ƒ } |  j j | | f ƒ | Dd Hn |  j | ƒ d S)z†Put an item into the queue.

        If you yield from put(), wait until a free slot is available
        before adding item.
        z)queue non-empty, why are getters waiting?r   r   N)r/   r   r   ÚAssertionErrorr   r#   Ú
set_resultr    r   r1   r
   ÚFuturer   r   r!   )r   r"   ÚgetterÚwaiterr   r   r   Úputn   s    
		$z	Queue.putc             C   s™   |  j  ƒ  |  j r[ |  j s) t d ƒ ‚ |  j j ƒ  } |  j | ƒ | j |  j ƒ  ƒ n: |  j d k rˆ |  j |  j	 ƒ  k rˆ t
 ‚ n |  j | ƒ d S)zyPut an item into the queue without blocking.

        If no free slot is immediately available, raise QueueFull.
        z)queue non-empty, why are getters waiting?r   N)r/   r   r   r4   r   r#   r5   r    r   r1   r   )r   r"   r7   r   r   r   Ú
put_nowaitŠ   s    
		$	zQueue.put_nowaitc             c   sµ   |  j  ƒ  |  j rm |  j ƒ  s+ t d ƒ ‚ |  j j ƒ  \ } } |  j | ƒ |  j j | j d ƒ |  j	 ƒ  S|  j
 ƒ  rƒ |  j	 ƒ  St j d |  j ƒ } |  j j | ƒ | Dd HSd S)ztRemove and return an item from the queue.

        If you yield from get(), wait until a item is available.
        z(queue not full, why are putters waiting?Nr   )r0   r   r3   r4   r   r#   r   Z	call_soonZ_set_result_unless_cancelledr    r1   r
   r6   r   r!   )r   r"   Úputterr8   r   r   r   Úget    s    
	

z	Queue.getc             C   s„   |  j  ƒ  |  j rd |  j ƒ  s+ t d ƒ ‚ |  j j ƒ  \ } } |  j | ƒ | j d ƒ |  j ƒ  S|  j ƒ  rz |  j ƒ  St	 ‚ d S)z‚Remove and return an item from the queue.

        Return an item if one is immediately available, else raise QueueEmpty.
        z(queue not full, why are putters waiting?N)
r0   r   r3   r4   r   r#   r5   r    r1   r   )r   r"   r;   r   r   r   Ú
get_nowait¼   s    
	

zQueue.get_nowait)r   r   r   r   r   r   r    r#   r(   r)   r'   r/   r0   r1   Úpropertyr   r2   r3   r   r9   r:   r<   r=   r   r   r   r   r      s$   

c               @   sF   e  Z d  Z d Z d d „  Z e j d d „ Z e j d d „ Z	 d S)	r   z•A subclass of Queue; retrieves entries in priority order (lowest first).

    Entries are typically tuples of the form: (priority number, data).
    c             C   s   g  |  _  d  S)N)r   )r   r   r   r   r   r   ×   s    zPriorityQueue._initc             C   s   | |  j  | ƒ d  S)N)r   )r   r"   Úheappushr   r   r   r#   Ú   s    zPriorityQueue._putc             C   s   | |  j  ƒ S)N)r   )r   Úheappopr   r   r   r    Ý   s    zPriorityQueue._getN)
r   r   r   r   r   Úheapqr?   r#   r@   r    r   r   r   r   r   Ñ   s   c               @   s:   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d S)	r   zEA subclass of Queue that retrieves most recently added entries first.c             C   s   g  |  _  d  S)N)r   )r   r   r   r   r   r   ä   s    zLifoQueue._initc             C   s   |  j  j | ƒ d  S)N)r   r!   )r   r"   r   r   r   r#   ç   s    zLifoQueue._putc             C   s   |  j  j ƒ  S)N)r   Úpop)r   r   r   r   r    ê   s    zLifoQueue._getN)r   r   r   r   r   r#   r    r   r   r   r   r   á   s   c                   sm   e  Z d  Z d Z d d d ‡  f d d †Z d d „  Z ‡  f d	 d
 †  Z d d „  Z e d d „  ƒ Z	 ‡  S)r   z8A subclass of Queue with task_done() and join() methods.r   r   Nc               sK   t  ƒ  j d | d | ƒ d |  _ t j d |  j ƒ |  _ |  j j ƒ  d  S)Nr   r   r   )Úsuperr   Ú_unfinished_tasksr   ZEventr   Ú	_finishedÚset)r   r   r   )Ú	__class__r   r   r   ñ   s    	zJoinableQueue.__init__c             C   s5   t  j |  ƒ } |  j r1 | d j |  j ƒ 7} n  | S)Nz	 tasks={})r   r'   rD   r$   )r   r-   r   r   r   r'   ÷   s    	zJoinableQueue._formatc                s0   t  ƒ  j | ƒ |  j d 7_ |  j j ƒ  d  S)Nr   )rC   r#   rD   rE   Úclear)r   r"   )rG   r   r   r#   ý   s    zJoinableQueue._putc             C   sP   |  j  d k r t d ƒ ‚ n  |  j  d 8_  |  j  d k rL |  j j ƒ  n  d S)a$  Indicate that a formerly enqueued task is complete.

        Used by queue consumers. 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 ValueError if called more times than there were items placed in
        the queue.
        r   z!task_done() called too many timesr   N)rD   Ú
ValueErrorrE   rF   )r   r   r   r   Ú	task_done  s
    zJoinableQueue.task_donec             c   s(   |  j  d k r$ |  j j ƒ  Dd Hn  d S)a†  Block 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 that the item was retrieved and all work on it is complete.
        When the count of unfinished tasks drops to zero, join() unblocks.
        r   N)rD   rE   Úwait)r   r   r   r   Újoin  s    	zJoinableQueue.join)
r   r   r   r   r   r'   r#   rJ   r   rL   r   r   )rG   r   r   î   s   )r   Ú__all__r   rA   Ú r	   r
   r   Ztasksr   Ú	Exceptionr   r   r   r   r   r   r   r   r   r   Ú<module>   s   ¸