ó
    Rì?gò0  ã                   ó„  • S r SSKrSSKrSSKrSSKJrJr  SSKJrJ	r	  SSK
Jr  SSKJrJrJrJrJr  SSKr\R$                  (       a
  SSKJrJrJr  \" S5      r/ S	Qr " S
 S\5      r " S S\5      rS\S\S\\R8                  4   SS4S jr " S S\\   5      r " S S\\   5      r " S S\5      r  " S S\5      r!g)aÆ  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.

é    N)ÚgenÚioloop)ÚFutureÚ"future_set_result_unless_cancelled)ÚEvent)ÚUnionÚTypeVarÚGenericÚ	AwaitableÚOptional)ÚDequeÚTupleÚAnyÚ_T)ÚQueueÚPriorityQueueÚ	LifoQueueÚ	QueueFullÚ
QueueEmptyc                   ó   • \ rS rSrSrSrg)r   é/   z:Raised by `.Queue.get_nowait` when the queue has no items.© N©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__static_attributes__r   ó    Ú0/usr/lib/python3/dist-packages/tornado/queues.pyr   r   /   s   † ÙDâr    r   c                   ó   • \ rS rSrSrSrg)r   é5   zBRaised by `.Queue.put_nowait` when a queue is at its maximum size.r   Nr   r   r    r!   r   r   5   s   † ÙLâr    r   ÚfutureÚtimeoutÚreturnc                 ó´   ^ ^^• U(       aN  SU 4S jjn[         R                  R                  5       mTR                  X5      mT R	                  UU4S j5        g g )Nc                  óz   >• T R                  5       (       d%  T R                  [        R                  " 5       5        g g ©N)ÚdoneÚset_exceptionr   ÚTimeoutError)r$   s   €r!   Ú
on_timeoutÚ _set_timeout.<locals>.on_timeout@   s*   ø€ Ø—;‘;—=‘=Ø×$Ñ$¤S×%5Ò%5Ó%7Õ8ð !r    c                 ó&   >• TR                  T5      $ r)   )Úremove_timeout)Ú_Úio_loopÚtimeout_handles    €€r!   Ú<lambda>Ú_set_timeout.<locals>.<lambda>F   s   ø€ ¨7×+AÑ+AÀ.Ô+Qr    ©r&   N)r   ÚIOLoopÚcurrentÚadd_timeoutÚadd_done_callback)r$   r%   r-   r2   r3   s   `  @@r!   Ú_set_timeoutr;   ;   sE   ú€ ö ÷	9ô —-‘-×'Ñ'Ó)ˆØ ×,Ñ,¨WÓAˆØ× Ñ Õ!QÕRð r    c                   ó2   • \ rS rSrSS jrS\\   4S jrSrg)Ú_QueueIteratoréI   r&   Nc                 ó   • Xl         g r)   ©Úq)ÚselfrA   s     r!   Ú__init__Ú_QueueIterator.__init__J   s   € Ø�r    c                 ó6   • U R                   R                  5       $ r)   )rA   Úget©rB   s    r!   Ú	__anext__Ú_QueueIterator.__anext__M   s   € Ø�v‰v�z‰z‹|Ðr    r@   )rA   z	Queue[_T]r&   N)	r   r   r   r   rC   r   r   rH   r   r   r    r!   r=   r=   I   s   † ôð˜9 R™=÷ r    r=   c                   óæ  • \ rS rSrSrSrSS\SS4S jjr\S\4S j5       r	S\4S jr
S\4S	 jrS\4S
 jr SS\S\\\\R&                  4      SS4S jjrS\SS4S jr SS\\\\R&                  4      S\\   4S jjrS\4S jrS S jr SS\\\\R&                  4      S\S   4S jjrS\\   4S jrS S jrS\4S jrS\SS4S jrS\SS4S jr S S jr!S\"4S jr#S\"4S jr$S\"4S jr%Sr&g)!r   éQ   aÝ  Coordinate producer and consumer coroutines.

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

.. testcode::

    import asyncio
    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 asyncio.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')

    asyncio.run(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.

NÚmaxsizer&   c                 ó:  • Uc  [        S5      eUS:  a  [        S5      eXl        U R                  5         [        R
                  " / 5      U l        [        R
                  " / 5      U l        SU l        [        5       U l
        U R                  R                  5         g )Nzmaxsize can't be Noner   zmaxsize can't be negative)Ú	TypeErrorÚ
ValueErrorÚ_maxsizeÚ_initÚcollectionsÚdequeÚ_gettersÚ_puttersÚ_unfinished_tasksr   Ú	_finishedÚset)rB   rL   s     r!   rC   ÚQueue.__init__™   sy   € Ø‰?ÜÐ3Ó4Ð4à�Q‹;ÜÐ8Ó9Ð9àŒØ�
‰
ŒÜ#×)Ò)¨"Ó-ˆŒÜ#×)Ò)¨"Ó-ˆŒØ!"ˆÔÜ›ˆŒØ�‰×ÑÕr    c                 ó   • U R                   $ )z%Number of items allowed in the queue.)rP   rG   s    r!   rL   ÚQueue.maxsize¨   s   € ð �}‰}Ðr    c                 ó,   • [        U R                  5      $ )zNumber of items in the queue.)ÚlenÚ_queuerG   s    r!   ÚqsizeÚQueue.qsize­   s   € ä�4—;‘;ÓÐr    c                 ó$   • U R                   (       + $ r)   ©r^   rG   s    r!   ÚemptyÚQueue.empty±   s   € Ø—;‘;ŒÐr    c                 ó^   • U R                   S:X  a  gU R                  5       U R                   :¬  $ )Nr   F)rL   r_   rG   s    r!   ÚfullÚ
Queue.full´   s&   € Ø�<‰<˜1ÓØà—:‘:“< 4§<¡<Ñ/Ð/r    Úitemr%   zFuture[None]c                 óÐ   • [        5       n U R                  U5        UR                  S5        U$ ! [         a+    U R                  R                  X45        [        X25         U$ f = f)aT  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   Ú
put_nowaitÚ
set_resultr   rU   Úappendr;   )rB   rh   r%   r$   s       r!   ÚputÚ	Queue.putº   se   € ô “ˆð	$Ø�O‰O˜DÔ!ð
 ×Ñ˜dÔ#Øˆøô ó 	*Ø�M‰M× Ñ  $ Ô0Ü˜Õ)ð ˆð	*ús   Œ0 °1A%Á$A%c                 ó`  • U R                  5         U R                  (       aa  U R                  5       (       d   S5       eU R                  R                  5       nU R	                  U5        [        X R                  5       5        gU R                  5       (       a  [        eU R	                  U5        g)zkPut an item into the queue without blocking.

If no free slot is immediately available, raise `QueueFull`.
z)queue non-empty, why are getters waiting?N)	Ú_consume_expiredrT   rc   ÚpopleftÚ_Queue__put_internalr   Ú_getrf   r   )rB   rh   Úgetters      r!   rj   ÚQueue.put_nowaitÑ   sy   € ð
 	×ÑÔØ�=�=Ø—:‘:—<‘<ÐLÐ!LÓL�<Ø—]‘]×*Ñ*Ó,ˆFØ×Ñ Ô%Ü.¨v·y±y³{ÕCØ�Y‰Y�[‰[ÜˆOà×Ñ Õ%r    c                 óÈ   • [        5       n UR                  U R                  5       5        U$ ! [         a*    U R                  R                  U5        [        X!5         U$ f = f)a¾  Remove and return an item from the queue.

Returns an awaitable 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.

.. note::

   The ``timeout`` argument of this method differs from that
   of the standard library's `queue.Queue.get`. That method
   interprets numeric values as relative timeouts; this one
   interprets them as absolute deadlines and requires
   ``timedelta`` objects for relative timeouts (consistent
   with other timeouts in Tornado).

)r   rk   Ú
get_nowaitr   rT   rl   r;   )rB   r%   r$   s      r!   rF   Ú	Queue.getá   s\   € ô. “ˆð	*Ø×Ñ˜dŸo™oÓ/Ô0ð ˆøô ó 	*Ø�M‰M× Ñ  Ô(Ü˜Õ)Øˆð	*ús   Œ- ­0A!Á A!c                 ód  • U R                  5         U R                  (       ae  U R                  5       (       d   S5       eU R                  R                  5       u  pU R	                  U5        [        US5        U R                  5       $ U R                  5       (       a  U R                  5       $ [        e)z…Remove and return an item from the queue without blocking.

Return an item if one is immediately available, else raise
`QueueEmpty`.
z(queue not full, why are putters waiting?N)	rp   rU   rf   rq   rr   r   rs   r_   r   )rB   rh   Úputters      r!   rw   ÚQueue.get_nowait   s‚   € ð 	×ÑÔØ�=�=Ø—9‘9—;‘;ÐJÐ JÓJ�;ØŸ=™=×0Ñ0Ó2‰LˆDØ×Ñ Ô%Ü.¨v°tÔ<Ø—9‘9“;ÐØ�Z‰Z�\‰\Ø—9‘9“;ÐäÐr    c                 óº   • U R                   S::  a  [        S5      eU =R                   S-  sl         U R                   S:X  a  U R                  R                  5         gg)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 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`.
r   z!task_done() called too many timesé   N)rV   rO   rW   rX   rG   s    r!   Ú	task_doneÚQueue.task_done  sR   € ð ×!Ñ! QÓ&ÜÐ@ÓAÐAØ×Ò !Ñ#ÕØ×!Ñ! QÓ&Ø�N‰N×ÑÕ ð 'r    c                 ó8   • U R                   R                  U5      $ )zƒBlock until all items in the queue are processed.

Returns an awaitable, which raises `tornado.util.TimeoutError` after a
timeout.
)rW   Úwait)rB   r%   s     r!   ÚjoinÚ
Queue.join$  s   € ð �~‰~×"Ñ" 7Ó+Ð+r    c                 ó   • [        U 5      $ r)   )r=   rG   s    r!   Ú	__aiter__ÚQueue.__aiter__.  s   € Ü˜dÓ#Ð#r    c                 ó8   • [         R                  " 5       U l        g r)   )rR   rS   r^   rG   s    r!   rQ   ÚQueue._init2  s   € Ü!×'Ò'Ó)ˆ�r    c                 ó6   • U R                   R                  5       $ r)   )r^   rq   rG   s    r!   rs   Ú
Queue._get5  s   € Ø�{‰{×"Ñ"Ó$Ð$r    c                 ó:   • U R                   R                  U5        g r)   ©r^   rl   ©rB   rh   s     r!   Ú_putÚ
Queue._put8  ó   € Ø�‰×Ñ˜4Õ r    c                 ó„   • U =R                   S-  sl         U R                  R                  5         U R                  U5        g )Nr}   )rV   rW   ÚclearrŽ   r�   s     r!   Ú__put_internalÚQueue.__put_internal=  s.   € Ø×Ò !Ñ#ÕØ�‰×ÑÔØ�	‰	�$�r    c                 ó  • U R                   (       aw  U R                   S   S   R                  5       (       aR  U R                   R                  5         U R                   (       a'  U R                   S   S   R                  5       (       a  MR  U R                  (       at  U R                  S   R                  5       (       aQ  U R                  R                  5         U R                  (       a%  U R                  S   R                  5       (       a  MO  g g g g )Nr   r}   )rU   r*   rq   rT   rG   s    r!   rp   ÚQueue._consume_expiredB  s±   € à�m�m §¡¨aÑ 0°Ñ 3× 8Ñ 8× :Ñ :Ø�M‰M×!Ñ!Ô#ð �m�m §¡¨aÑ 0°Ñ 3× 8Ñ 8× :Ó :ð �m�m §¡¨aÑ 0× 5Ñ 5× 7Ñ 7Ø�M‰M×!Ñ!Ô#ð �m�m §¡¨aÑ 0× 5Ñ 5× 7Ô 7ˆmÐ 7ˆmr    c                 ó†   • S[        U 5      R                  < S[        [        U 5      5      < SU R	                  5       < S3$ )NÚ<z at Ú Ú>)Útyper   ÚhexÚidÚ_formatrG   s    r!   Ú__repr__ÚQueue.__repr__J  s'   � Ü"& t£*×"5Ô"5´s¼2¸d»8¶}ÀdÇlÁlÆnÐUÐUr    c                 óZ   • S[        U 5      R                  < SU R                  5       < S3$ )Nr˜   r™   rš   )r›   r   rž   rG   s    r!   Ú__str__ÚQueue.__str__M  s   � Ü  ›J×/Ô/°·±¶Ð@Ð@r    c                 ód  • SU R                   < 3n[        U SS 5      (       a  USU R                  -  -  nU R                  (       a  US[	        U R                  5      -  -  nU R
                  (       a  US[	        U R
                  5      -  -  nU R                  (       a  USU R                  -  -  nU$ )Nzmaxsize=r^   z	 queue=%rz getters[%s]z putters[%s]z	 tasks=%s)rL   Úgetattrr^   rT   r]   rU   rV   )rB   Úresults     r!   rž   ÚQueue._formatP  s‘   � Ø!%§£Ð/ˆÜ�4˜ 4×(Ñ(Ø�k D§K¡KÑ/Ñ/ˆFØ�=�=Ø�n¤s¨4¯=©=Ó'9Ñ9Ñ9ˆFØ�=�=Ø�n¤s¨4¯=©=Ó'9Ñ9Ñ9ˆFØ×!×!Ø�k D×$:Ñ$:Ñ:Ñ:ˆFØˆr    )rW   rT   rP   rU   r^   rV   )r   r)   r6   )'r   r   r   r   r   r^   ÚintrC   ÚpropertyrL   r_   Úboolrc   rf   r   r   r   ÚfloatÚdatetimeÚ	timedeltarm   rj   r   rF   rw   r~   r‚   r=   r…   rQ   rs   rŽ   rr   rp   ÚstrrŸ   r¢   rž   r   r   r    r!   r   r   Q   sž  † ñAðJ €Fñ ð ¨Dõ ð ð˜ó ó ðð �sô  ð�tô ð0�dô 0ð OSñØðØ!)¨%°°x×7IÑ7IÐ0IÑ*JÑ!Kðà	õð.&˜rð & dô &ð" EIñØ  e¨X×-?Ñ-?Ð&?Ñ @ÑAðà	�2‰õð>˜Bô ô$!ð& EIñ,Ø  e¨X×-?Ñ-?Ð&?Ñ @ÑAð,à	�4‰õ,ð$˜>¨"Ñ-ô $ô*ð%�bô %ð!˜ð ! ô !ð
 2ð ¨$ô ô
$ðV˜#ô VðA˜ô Að
˜÷ 
r    r   c                   óB   • \ rS rSrSrS
S jrS\SS4S jrS\4S jrS	r	g)r   i]  ab  A `.Queue` that retrieves entries in priority order, lowest first.

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

.. testcode::

    import asyncio
    from tornado.queues import PriorityQueue

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

        print(await q.get())
        print(await q.get())
        print(await q.get())

    asyncio.run(main())

.. testoutput::

    (0, 'high-priority item')
    (1, 'medium-priority item')
    (10, 'low-priority item')
r&   Nc                 ó   • / U l         g r)   rb   rG   s    r!   rQ   ÚPriorityQueue._initz  ó	   € Øˆ�r    rh   c                 óF   • [         R                  " U R                  U5        g r)   )ÚheapqÚheappushr^   r�   s     r!   rŽ   ÚPriorityQueue._put}  s   € Ü�Š�t—{‘{ DÕ)r    c                 óB   • [         R                  " U R                  5      $ r)   )r´   Úheappopr^   rG   s    r!   rs   ÚPriorityQueue._get€  s   € Ü�}Š}˜TŸ[™[Ó)Ð)r    rb   r6   ©
r   r   r   r   r   rQ   r   rŽ   rs   r   r   r    r!   r   r   ]  s+   † ñô8ð*˜ð * ô *ð*�b÷ *r    r   c                   óB   • \ rS rSrSrS
S jrS\SS4S jrS\4S jrS	r	g)r   i„  a€  A `.Queue` that retrieves the most recently put items first.

.. testcode::

    import asyncio
    from tornado.queues import LifoQueue

    async def main():
        q = LifoQueue()
        q.put(3)
        q.put(2)
        q.put(1)

        print(await q.get())
        print(await q.get())
        print(await q.get())

    asyncio.run(main())

.. testoutput::

    1
    2
    3
r&   Nc                 ó   • / U l         g r)   rb   rG   s    r!   rQ   ÚLifoQueue._initŸ  r²   r    rh   c                 ó:   • U R                   R                  U5        g r)   rŒ   r�   s     r!   rŽ   ÚLifoQueue._put¢  r�   r    c                 ó6   • U R                   R                  5       $ r)   )r^   ÚpoprG   s    r!   rs   ÚLifoQueue._get¥  s   € Ø�{‰{�‰Ó Ð r    rb   r6   rº   r   r    r!   r   r   „  s+   † ñô4ð!˜ð ! ô !ð!�b÷ !r    r   )"r   rR   r¬   r´   Útornador   r   Útornado.concurrentr   r   Útornado.locksr   Útypingr   r	   r
   r   r   ÚTYPE_CHECKINGr   r   r   r   Ú__all__Ú	Exceptionr   r   r«   r­   r;   r=   r   r   r   r   r    r!   Ú<module>rÊ      sË   ðñó Û Û ç ß IÝ ç ?Õ ?Û à	××ß(Ñ(áˆTƒ]€â
L€ô	�ô 	ô	�	ô 	ðSØðSØ" 4¨°×0BÑ0BÐ#BÑCðSà	ôSô�W˜R‘[ô ôIˆG�B‰Kô IôX$*�Eô $*ôN"!�õ "!r    