σ
¨[c           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l m Z m	 Z	 d d l
 m Z m Z d d l m Z d d d	 d
 d g Z d e f d     YZ d
 e f d     YZ d   Z d e f d     YZ d e f d     YZ d e f d     YZ d	 e f d     YZ d S(   sΖ  Asynchronous queues for coroutines. These classes are very similar
to those provided in the standard library's `asyncio package
<https://docs.python.org/3/library/asyncio-queue.html>`_.

.. warning::

   Unlike the standard library's `queue` module, the classes defined here
   are *not* thread-safe. To use these queues from another thread,
   use `.IOLoop.add_callback` to transfer control to the `.IOLoop` thread
   before calling any queue methods.

i    (   t   absolute_importt   divisiont   print_functionN(   t   gent   ioloop(   t   Futuret"   future_set_result_unless_cancelled(   t   Eventt   Queuet   PriorityQueuet	   LifoQueuet	   QueueFullt
   QueueEmptyc           B` s   e  Z d  Z RS(   s:   Raised by `.Queue.get_nowait` when the queue has no items.(   t   __name__t
   __module__t   __doc__(    (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR   (   s   c           B` s   e  Z d  Z RS(   sB   Raised by `.Queue.put_nowait` when a queue is at its maximum size.(   R   R   R   (    (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR   -   s   c         ` sV   | rR   f d   } t  j j     j | |     j   f d    n  d  S(   Nc           ` s&     j    s"   j t j    n  d  S(   N(   t   donet   set_exceptionR   t   TimeoutError(    (   t   future(    s-   lib/python2.7/site-packages/tornado/queues.pyt
   on_timeout4   s    c         ` s     j    S(   N(   t   remove_timeout(   t   _(   t   io_loopt   timeout_handle(    s-   lib/python2.7/site-packages/tornado/queues.pyt   <lambda>:   s    (   R   t   IOLoopt   currentt   add_timeoutt   add_done_callback(   R   t   timeoutR   (    (   R   R   R   s-   lib/python2.7/site-packages/tornado/queues.pyt   _set_timeout2   s    t   _QueueIteratorc           B` s   e  Z d    Z d   Z RS(   c         C` s   | |  _  d  S(   N(   t   q(   t   selfR!   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   __init__>   s    c         C` s   |  j  j   S(   N(   R!   t   get(   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt	   __anext__A   s    (   R   R   R#   R%   (    (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR    =   s   	c           B` sΤ   e  Z d  Z d d  Z e d    Z d   Z d   Z d   Z d d  Z
 d   Z d d	  Z d
   Z d   Z d d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   sΆ  Coordinate producer and consumer coroutines.

    If maxsize is 0 (the default) the queue size is unbounded.

    .. testcode::

        from tornado import gen
        from tornado.ioloop import IOLoop
        from tornado.queues import Queue

        q = Queue(maxsize=2)

        async def consumer():
            async for item in q:
                try:
                    print('Doing work on %s' % item)
                    await gen.sleep(0.01)
                finally:
                    q.task_done()

        async def producer():
            for item in range(5):
                await q.put(item)
                print('Put %s' % item)

        async def main():
            # Start consumer without waiting (since it never finishes).
            IOLoop.current().spawn_callback(consumer)
            await producer()     # Wait for producer to put all tasks.
            await q.join()       # Wait for consumer to finish all tasks.
            print('Done')

        IOLoop.current().run_sync(main)

    .. testoutput::

        Put 0
        Put 1
        Doing work on 0
        Put 2
        Doing work on 1
        Put 3
        Doing work on 2
        Put 4
        Doing work on 3
        Doing work on 4
        Done


    In versions of Python without native coroutines (before 3.5),
    ``consumer()`` could be written as::

        @gen.coroutine
        def consumer():
            while True:
                item = yield q.get()
                try:
                    print('Doing work on %s' % item)
                    yield gen.sleep(0.01)
                finally:
                    q.task_done()

    .. versionchanged:: 4.3
       Added ``async for`` support in Python 3.5.

    i    c         C` s   | d  k r t d   n  | d k  r6 t d   n  | |  _ |  j   t j g   |  _ t j g   |  _ d |  _	 t
   |  _ |  j j   d  S(   Ns   maxsize can't be Nonei    s   maxsize can't be negative(   t   Nonet	   TypeErrort
   ValueErrort   _maxsizet   _initt   collectionst   dequet   _getterst   _putterst   _unfinished_tasksR   t	   _finishedt   set(   R"   t   maxsize(    (    s-   lib/python2.7/site-packages/tornado/queues.pyR#      s    	
	c         C` s   |  j  S(   s%   Number of items allowed in the queue.(   R)   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR2      s    c         C` s   t  |  j  S(   s   Number of items in the queue.(   t   lent   _queue(   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   qsize   s    c         C` s   |  j  S(   N(   R4   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   empty    s    c         C` s*   |  j  d k r t S|  j   |  j  k Sd  S(   Ni    (   R2   t   FalseR5   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   full£   s    c         C` sb   t    } y |  j |  Wn4 t k
 rP |  j j | | f  t | |  n X| j d  | S(   s  Put an item into the queue, perhaps waiting until there is room.

        Returns a Future, which raises `tornado.util.TimeoutError` after a
        timeout.

        ``timeout`` may be a number denoting a time (on the same
        scale as `tornado.ioloop.IOLoop.time`, normally `time.time`), or a
        `datetime.timedelta` object for a deadline relative to the
        current time.
        N(   R   t
   put_nowaitR   R.   t   appendR   t
   set_resultR&   (   R"   t   itemR   R   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   put©   s    	c         C` s   |  j    |  j r] |  j   s+ t d   |  j j   } |  j |  t | |  j    n" |  j   rr t	  n |  j |  d S(   s{   Put an item into the queue without blocking.

        If no free slot is immediately available, raise `QueueFull`.
        s)   queue non-empty, why are getters waiting?N(
   t   _consume_expiredR-   R6   t   AssertionErrort   popleftt   _Queue__put_internalR   t   _getR8   R   (   R"   R<   t   getter(    (    s-   lib/python2.7/site-packages/tornado/queues.pyR9   Ύ   s    
		c         C` sU   t    } y | j |  j    Wn. t k
 rP |  j j |  t | |  n X| S(   s  Remove and return an item from the queue.

        Returns a Future which resolves once an item is available, or raises
        `tornado.util.TimeoutError` after a timeout.

        ``timeout`` may be a number denoting a time (on the same
        scale as `tornado.ioloop.IOLoop.time`, normally `time.time`), or a
        `datetime.timedelta` object for a deadline relative to the
        current time.
        (   R   R;   t
   get_nowaitR   R-   R:   R   (   R"   R   R   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR$   Ξ   s    	c         C` s   |  j    |  j rd |  j   s+ t d   |  j j   \ } } |  j |  t | d  |  j   S|  j	   rz |  j   St
  d S(   s   Remove and return an item from the queue without blocking.

        Return an item if one is immediately available, else raise
        `QueueEmpty`.
        s(   queue not full, why are putters waiting?N(   R>   R.   R8   R?   R@   RA   R   R&   RB   R5   R   (   R"   R<   t   putter(    (    s-   lib/python2.7/site-packages/tornado/queues.pyRD   α   s    
	

c         C` sP   |  j  d k r t d   n  |  j  d 8_  |  j  d k rL |  j j   n  d S(   sΕ  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 blocking, it resumes when all items have been
        processed; that is, when every `.put` is matched by a `.task_done`.

        Raises `ValueError` if called more times than `.put`.
        i    s!   task_done() called too many timesi   N(   R/   R(   R0   R1   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt	   task_doneσ   s
    c         C` s   |  j  j |  S(   s   Block until all items in the queue are processed.

        Returns a Future, which raises `tornado.util.TimeoutError` after a
        timeout.
        (   R0   t   wait(   R"   R   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   join  s    c         C` s
   t  |   S(   N(   R    (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt	   __aiter__  s    c         C` s   t  j   |  _ d  S(   N(   R+   R,   R4   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR*     s    c         C` s   |  j  j   S(   N(   R4   R@   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyRB     s    c         C` s   |  j  j |  d  S(   N(   R4   R:   (   R"   R<   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   _put  s    c         C` s-   |  j  d 7_  |  j j   |  j |  d  S(   Ni   (   R/   R0   t   clearRJ   (   R"   R<   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   __put_internal  s    c         C` sh   x1 |  j  r3 |  j  d d j   r3 |  j  j   q Wx- |  j rc |  j d j   rc |  j j   q7 Wd  S(   Ni    i   (   R.   R   R@   R-   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR>      s    #c         C` s,   d t  |   j t t |    |  j   f S(   Ns   <%s at %s %s>(   t   typeR   t   hext   idt   _format(   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   __repr__(  s    c         C` s   d t  |   j |  j   f S(   Ns   <%s %s>(   RM   R   RP   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   __str__,  s    c         C` s   d |  j  f } t |  d d   r6 | d |  j 7} n  |  j rY | d t |  j  7} n  |  j r| | d t |  j  7} n  |  j r | d |  j 7} n  | S(   Ns
   maxsize=%rR4   s	    queue=%rs    getters[%s]s    putters[%s]s	    tasks=%s(   R2   t   getattrR&   R4   R-   R3   R.   R/   (   R"   t   result(    (    s-   lib/python2.7/site-packages/tornado/queues.pyRP   /  s    			N(   R   R   R   R#   t   propertyR2   R5   R6   R8   R&   R=   R9   R$   RD   RF   RH   RI   R*   RB   RJ   RA   R>   RQ   RR   RP   (    (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR   E   s*   B														c           B` s)   e  Z d  Z d   Z d   Z d   Z RS(   sC  A `.Queue` that retrieves entries in priority order, lowest first.

    Entries are typically tuples like ``(priority number, data)``.

    .. testcode::

        from tornado.queues import PriorityQueue

        q = PriorityQueue()
        q.put((1, 'medium-priority item'))
        q.put((0, 'high-priority item'))
        q.put((10, 'low-priority item'))

        print(q.get_nowait())
        print(q.get_nowait())
        print(q.get_nowait())

    .. testoutput::

        (0, 'high-priority item')
        (1, 'medium-priority item')
        (10, 'low-priority item')
    c         C` s   g  |  _  d  S(   N(   R4   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR*   T  s    c         C` s   t  j |  j |  d  S(   N(   t   heapqt   heappushR4   (   R"   R<   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyRJ   W  s    c         C` s   t  j |  j  S(   N(   RV   t   heappopR4   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyRB   Z  s    (   R   R   R   R*   RJ   RB   (    (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR	   <  s   		c           B` s)   e  Z d  Z d   Z d   Z d   Z RS(   s]  A `.Queue` that retrieves the most recently put items first.

    .. testcode::

        from tornado.queues import LifoQueue

        q = LifoQueue()
        q.put(3)
        q.put(2)
        q.put(1)

        print(q.get_nowait())
        print(q.get_nowait())
        print(q.get_nowait())

    .. testoutput::

        1
        2
        3
    c         C` s   g  |  _  d  S(   N(   R4   (   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR*   t  s    c         C` s   |  j  j |  d  S(   N(   R4   R:   (   R"   R<   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyRJ   w  s    c         C` s   |  j  j   S(   N(   R4   t   pop(   R"   (    (    s-   lib/python2.7/site-packages/tornado/queues.pyRB   z  s    (   R   R   R   R*   RJ   RB   (    (    (    s-   lib/python2.7/site-packages/tornado/queues.pyR
   ^  s   		(   R   t
   __future__R    R   R   R+   RV   t   tornadoR   R   t   tornado.concurrentR   R   t   tornado.locksR   t   __all__t	   ExceptionR   R   R   t   objectR    R   R	   R
   (    (    (    s-   lib/python2.7/site-packages/tornado/queues.pyt   <module>   s   	χ"