Ë
    êmxiá0  ã                   óx  — d Z ddl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lmZmZmZmZmZ ddlZej$                  r
ddlmZmZmZ  ed«      Zg d	¢Z G d
„ de«      Z G d„ de«      Zdededeej8                  f   ddfd„Z G d„ dee   «      Z G d„ dee   «      Z G d„ de«      Z  G d„ de«      Z!y)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                   ó   — e Zd ZdZy)r   z:Raised by `.Queue.get_nowait` when the queue has no items.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    úE/home/htdocs/ttos/venv/lib/python3.12/site-packages/tornado/queues.pyr   r   /   s   „ ÙDàr   r   c                   ó   — e Zd ZdZy)r   zBRaised by `.Queue.put_nowait` when a queue is at its maximum size.Nr   r   r   r   r   r   5   s   „ ÙLàr   r   ÚfutureÚtimeoutÚreturnc                 ó¦   ‡ ‡‡— |rLdˆ fd„}t         j                  j                  «       Š‰j                  ||«      Š‰ j	                  ˆˆfd„«       y y )Nc                  ón   •— ‰ j                  «       s$‰ j                  t        j                  «       «       y y ©N)ÚdoneÚset_exceptionr   ÚTimeoutError)r    s   €r   Ú
on_timeoutz _set_timeout.<locals>.on_timeout@   s(   ø€ Ø—;‘;”=Ø×$Ñ$¤S×%5Ñ%5Ó%7Õ8ð !r   c                 ó&   •— ‰j                  ‰«      S r%   )Úremove_timeout)Ú_Úio_loopÚtimeout_handles    €€r   ú<lambda>z_set_timeout.<locals>.<lambda>F   s   ø€ ¨7×+AÑ+AÀ.Ó+Q€ r   ©r"   N)r   ÚIOLoopÚcurrentÚadd_timeoutÚadd_done_callback)r    r!   r)   r-   r.   s   `  @@r   Ú_set_timeoutr5   ;   sG   ú€ ñ õ	9ô —-‘-×'Ñ'Ó)ˆØ ×,Ñ,¨W°jÓAˆØ× Ñ Ô!QÕRð r   c                   ó&   — e Zd Zdd„Zdee   fd„Zy)Ú_QueueIteratorr"   Nc                 ó   — || _         y r%   )Úq)Úselfr9   s     r   Ú__init__z_QueueIterator.__init__J   s	   € Øˆ�r   c                 ó6   — | j                   j                  «       S r%   )r9   Úget©r:   s    r   Ú	__anext__z_QueueIterator.__anext__M   s   € Ø�v‰v�z‰z‹|Ðr   )r9   z	Queue[_T]r"   N)r   r   r   r;   r   r   r?   r   r   r   r7   r7   I   s   „ óð˜9 R™=ô r   r7   c                   ó®  — e Zd ZdZdZddeddfd„Zedefd„«       Zdefd„Z	de
fd„Zde
fd	„Z	 dd
edeeeej$                  f      ddfd„Zd
eddfd„Z	 ddeeeej$                  f      dee   fd„Zdefd„Zdd„Z	 ddeeeej$                  f      ded   fd„Zdee   fd„Zdd„Zdefd„Zd
eddfd„Zd
eddfd„Zdd„Z de!fd„Z"de!fd„Z#de!fd„Z$y)r   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                 ó4  — |€t        d«      ‚|dk  rt        d«      ‚|| _        | j                  «        t	        j
                  g «      | _        t	        j
                  g «      | _        d| _        t        «       | _
        | j                  j                  «        y )Nzmaxsize can't be Noner   zmaxsize can't be negative)Ú	TypeErrorÚ
ValueErrorÚ_maxsizeÚ_initÚcollectionsÚdequeÚ_gettersÚ_puttersÚ_unfinished_tasksr   Ú	_finishedÚset)r:   rA   s     r   r;   zQueue.__init__™   s{   € Øˆ?ÜÐ3Ó4Ð4à�QŠ;ÜÐ8Ó9Ð9àˆŒØ�
‰
ŒÜ#×)Ñ)¨"Ó-ˆŒÜ#×)Ñ)¨"Ó-ˆŒØ!"ˆÔÜ›ˆŒØ�‰×ÑÕr   c                 ó   — | j                   S )z%Number of items allowed in the queue.)rE   r>   s    r   rA   zQueue.maxsize¨   s   € ð �}‰}Ðr   c                 ó,   — t        | j                  «      S )zNumber of items in the queue.)ÚlenÚ_queuer>   s    r   ÚqsizezQueue.qsize­   s   € ä�4—;‘;ÓÐr   c                 ó   — | j                    S r%   ©rQ   r>   s    r   ÚemptyzQueue.empty±   s   € Ø—;‘;ˆÐr   c                 ó\   — | j                   dk(  ry| j                  «       | j                   k\  S )Nr   F)rA   rR   r>   s    r   Úfullz
Queue.full´   s&   € Ø�<‰<˜1ÒØà—:‘:“< 4§<¡<Ñ/Ð/r   Úitemr!   zFuture[None]c                 óÒ   — t        «       }	 | j                  |«       |j                  d«       |S # t        $ r- | j                  j                  ||f«       t        ||«       Y |S w xY w)aŒ  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   rJ   Úappendr5   )r:   rX   r!   r    s       r   Úputz	Queue.putº   si   € ô “ˆð	$Ø�O‰O˜DÔ!ð
 ×Ñ˜dÔ#Øˆøô ò 	*Ø�M‰M× Ñ  $¨ Ô0Ü˜ Õ)ð ˆð	*ús   Œ0 °2A&Á%A&c                 óD  — | j                  «        | j                  r]| j                  «       sJ d«       ‚| j                  j                  «       }| j	                  |«       t        || j                  «       «       y| j                  «       rt        ‚| j	                  |«       y)z{Put 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_expiredrI   rU   ÚpopleftÚ_Queue__put_internalr   Ú_getrW   r   )r:   rX   Úgetters      r   rZ   zQueue.put_nowaitÑ   sw   € ð
 	×ÑÔØ�=Š=Ø—:‘:”<ÐLÐ!LÓL�<Ø—]‘]×*Ñ*Ó,ˆFØ×Ñ Ô%Ü.¨v°t·y±y³{ÕCØ�Y‰YŒ[ÜˆOà×Ñ Õ%r   c                 óÈ   — t        «       }	 |j                  | j                  «       «       |S # t        $ r+ | j                  j                  |«       t        ||«       Y |S w xY w)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   r[   Ú
get_nowaitr   rI   r\   r5   )r:   r!   r    s      r   r=   z	Queue.getá   s^   € ô. “ˆð	*Ø×Ñ˜dŸo™oÓ/Ô0ð ˆøô ò 	*Ø�M‰M× Ñ  Ô(Ü˜ Õ)Øˆð	*ús   Œ- ­0A!Á A!c                 óH  — | j                  «        | j                  ra| j                  «       sJ d«       ‚| j                  j                  «       \  }}| j	                  |«       t        |d«       | j                  «       S | j                  «       r| j                  «       S t        ‚)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)	r_   rJ   rW   r`   ra   r   rb   rR   r   )r:   rX   Úputters      r   re   zQueue.get_nowait   s€   € ð 	×ÑÔØ�=Š=Ø—9‘9”;ÐJÐ JÓJ�;ØŸ=™=×0Ñ0Ó2‰LˆD�&Ø×Ñ Ô%Ü.¨v°tÔ<Ø—9‘9“;ÐØ�Z‰ZŒ\Ø—9‘9“;ÐäÐr   c                 ó¶   — | j                   dk  rt        d«      ‚| xj                   dz  c_         | j                   dk(  r| j                  j                  «        yy)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)rK   rD   rL   rM   r>   s    r   Ú	task_donezQueue.task_done  sR   € ð ×!Ñ! QÒ&ÜÐ@ÓAÐAØ×Ò !Ñ#ÕØ×!Ñ! QÒ&Ø�N‰N×ÑÕ ð 'r   c                 ó8   — | j                   j                  |«      S )z›Block until all items in the queue are processed.

        Returns an awaitable, which raises `tornado.util.TimeoutError` after a
        timeout.
        )rL   Úwait)r:   r!   s     r   Újoinz
Queue.join$  s   € ð �~‰~×"Ñ" 7Ó+Ð+r   c                 ó   — t        | «      S r%   )r7   r>   s    r   Ú	__aiter__zQueue.__aiter__.  s   € Ü˜dÓ#Ð#r   c                 ó6   — t        j                  «       | _        y r%   )rG   rH   rQ   r>   s    r   rF   zQueue._init2  s   € Ü!×'Ñ'Ó)ˆ�r   c                 ó6   — | j                   j                  «       S r%   )rQ   r`   r>   s    r   rb   z
Queue._get5  s   € Ø�{‰{×"Ñ"Ó$Ð$r   c                 ó:   — | j                   j                  |«       y r%   ©rQ   r\   ©r:   rX   s     r   Ú_putz
Queue._put8  ó   € Ø�‰×Ñ˜4Õ r   c                 ó„   — | xj                   dz  c_         | j                  j                  «        | j                  |«       y )Nri   )rK   rL   Úclearru   rt   s     r   Ú__put_internalzQueue.__put_internal=  s.   € Ø×Ò !Ñ#ÕØ�‰×ÑÔØ�	‰	�$�r   c                 óÊ  — | j                   rg| j                   d   d   j                  «       rG| j                   j                  «        | j                   r!| j                   d   d   j                  «       rŒG| j                  rd| j                  d   j                  «       rF| j                  j                  «        | j                  r| j                  d   j                  «       rŒDy y y y )Nr   ri   )rJ   r&   r`   rI   r>   s    r   r_   z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Ø�M‰M×!Ñ!Ô#ð �mŠm §¡¨aÑ 0× 5Ñ 5Ö 7ˆmÐ 7ˆmr   c                 ó€   — dt        | «      j                  › dt        t        | «      «      › d| j	                  «       › d�S )Nú<z at ú ú>)Útyper   ÚhexÚidÚ_formatr>   s    r   Ú__repr__zQueue.__repr__J  s7   € Ø”4˜“:×&Ñ&Ð' t¬C´°4³«M¨?¸!¸D¿L¹L»NÐ;KÈ1ÐMÐMr   c                 óV   — dt        | «      j                  › d| j                  «       › d�S )Nr|   r}   r~   )r   r   r‚   r>   s    r   Ú__str__zQueue.__str__M  s)   € Ø”4˜“:×&Ñ&Ð' q¨¯©«Ð(8¸Ð:Ð:r   c                 ó:  — d| j                   ›�}t        | dd «      r|d| j                  z  z  }| j                  r|dt	        | j                  «      z  z  }| j
                  r|dt	        | j
                  «      z  z  }| j                  r|d| j                  z  z  }|S )Nzmaxsize=rQ   z	 queue=%rz getters[%s]z putters[%s]z	 tasks=%s)rA   ÚgetattrrQ   rI   rP   rJ   rK   )r:   Úresults     r   r‚   zQueue._formatP  s“   € Ø˜DŸL™LÐ+Ð,ˆÜ�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   )r   r%   r0   )%r   r   r   r   rQ   Úintr;   ÚpropertyrA   rR   ÚboolrU   rW   r   r   r   ÚfloatÚdatetimeÚ	timedeltar]   rZ   r   r=   re   rj   rm   r7   ro   rF   rb   ru   ra   r_   Ústrrƒ   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ð ¨$ó ó
$ðN˜#ó Nð;˜ó ;ð
˜ô 
r   r   c                   ó4   — e Zd ZdZdd„Zdeddfd„Zdefd„Zy)	r   aª  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                 ó   — g | _         y r%   rT   r>   s    r   rF   zPriorityQueue._initz  ó	   € Øˆ�r   rX   c                 óD   — t        j                  | j                  |«       y r%   )ÚheapqÚheappushrQ   rt   s     r   ru   zPriorityQueue._put}  s   € Ü�‰�t—{‘{ DÕ)r   c                 ó@   — t        j                  | j                  «      S r%   )r”   ÚheappoprQ   r>   s    r   rb   zPriorityQueue._get€  s   € Ü�}‰}˜TŸ[™[Ó)Ð)r   r0   ©r   r   r   r   rF   r   ru   rb   r   r   r   r   r   ]  s+   „ ñó8ð*˜ð * ó *ð*�bô *r   r   c                   ó4   — e Zd ZdZdd„Zdeddfd„Zdefd„Zy)	r   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                 ó   — g | _         y r%   rT   r>   s    r   rF   zLifoQueue._initŸ  r’   r   rX   c                 ó:   — | j                   j                  |«       y r%   rs   rt   s     r   ru   zLifoQueue._put¢  rv   r   c                 ó6   — | j                   j                  «       S r%   )rQ   Úpopr>   s    r   rb   zLifoQueue._get¥  s   € Ø�{‰{�‰Ó Ð r   r0   r˜   r   r   r   r   r   „  s+   „ ñó4ð!˜ð ! ó !ð!�bô !r   r   )"r   rG   r�   r”   Útornador   r   Útornado.concurrentr   r   Útornado.locksr   Útypingr   r	   r
   r   r   ÚTYPE_CHECKINGr   r   r   r   Ú__all__Ú	Exceptionr   r   rŒ   rŽ   r5   r7   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   