§
    ‹Ÿjá0  ã                   ó¢  — 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j        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!dS )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dS )r   z:Raised by `.Queue.get_nowait` when the queue has no items.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    ú[/var/www/finuniver-perm.ru/html/student/venv/lib/python3.11/site-packages/tornado/queues.pyr   r   /   s   € € € € € ØDÐDà€Dr   r   c                   ó   — e Zd ZdZdS )r   zBRaised by `.Queue.put_nowait` when a queue is at its maximum size.Nr   r   r   r   r   r   5   s   € € € € € ØLÐLà€Dr   r   ÚfutureÚtimeoutÚreturnc                 óº   ‡ ‡‡— |rUdˆ fd„}t           j                             ¦   «         Š‰                     ||¦  «        Š‰                      ˆˆfd„¦  «         d S d S )Nr"   c                  ó€   •— ‰                       ¦   «         s(‰                      t          j        ¦   «         ¦  «         d S d S ©N)ÚdoneÚset_exceptionr   ÚTimeoutError)r    s   €r   Ú
on_timeoutz _set_timeout.<locals>.on_timeout@   s@   ø€ Ø—;’;‘=”=ð 9Ø×$Ò$¥SÔ%5Ñ%7Ô%7Ñ8Ô8Ð8Ð8Ð8ð9ð 9r   c                 ó.   •— ‰                      ‰¦  «        S r%   )Úremove_timeout)Ú_Úio_loopÚtimeout_handles    €€r   ú<lambda>z_set_timeout.<locals>.<lambda>F   s   ø€ ¨7×+AÒ+AÀ.Ñ+QÔ+Q€ r   ©r"   N)r   ÚIOLoopÚcurrentÚadd_timeoutÚadd_done_callback)r    r!   r)   r-   r.   s   `  @@r   Ú_set_timeoutr5   ;   s‹   øøø€ ð ð Sð	9ð 	9ð 	9ð 	9ð 	9ð 	9õ ”-×'Ò'Ñ)Ô)ˆØ ×,Ò,¨W°jÑAÔAˆØ× Ò Ð!QÐ!QÐ!QÐ!QÐ!QÑRÔRÐRÐRÐRðSð Sr   c                   ó.   — e Zd Zdd„Zdee         fd„ZdS )Ú_QueueIteratorÚqú	Queue[_T]r"   Nc                 ó   — || _         d S r%   )r8   )Úselfr8   s     r   Ú__init__z_QueueIterator.__init__J   s   € ØˆŒˆˆr   c                 ó4   — | j                              ¦   «         S r%   )r8   Úget©r;   s    r   Ú	__anext__z_QueueIterator.__anext__M   s   € ØŒv�zŠz‰|Œ|Ðr   )r8   r9   r"   N)r   r   r   r<   r   r   r@   r   r   r   r7   r7   I   sG   € € € € € ðð ð ð ð˜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$dS ) 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.

    Nr   Úmaxsizer"   c                 óR  — |€t          d¦  «        ‚|dk     rt          d¦  «        ‚|| _        |                      ¦   «          t	          j        g ¦  «        | _        t	          j        g ¦  «        | _        d| _        t          ¦   «         | _
        | j
                             ¦   «          d S )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;   rB   s     r   r<   zQueue.__init__™   s“   € Øˆ?ÝÐ3Ñ4Ô4Ð4à�QŠ;ˆ;ÝÐ8Ñ9Ô9Ð9àˆŒØ�
Š
‰ŒˆÝ#Ô)¨"Ñ-Ô-ˆŒÝ#Ô)¨"Ñ-Ô-ˆŒØ!"ˆÔÝ™œˆŒØŒ×ÒÑÔÐÐÐr   c                 ó   — | j         S )z%Number of items allowed in the queue.)rF   r?   s    r   rB   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%   ©rR   r?   s    r   ÚemptyzQueue.empty±   s   € Ø”;ˆÐr   c                 óV   — | j         dk    rdS |                      ¦   «         | j         k    S )Nr   F)rB   rS   r?   s    r   Úfullz
Queue.full´   s)   € ØŒ<˜1ÒÐØ�5à—:’:‘<”< 4¤<Ò/Ð/r   Úitemr!   zFuture[None]c                 óò   — t          ¦   «         }	 |                      |¦  «         |                     d¦  «         n<# t          $ r/ | j                             ||f¦  «         t          ||¦  «         Y nw xY w|S )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   rK   Úappendr5   )r;   rY   r!   r    s       r   Úputz	Queue.putº   s‘   € õ ‘”ˆð	$Ø�OŠO˜DÑ!Ô!Ð!ð
 ×Ò˜dÑ#Ô#Ð#Ð#øõ	 ð 	*ð 	*ð 	*ØŒM× Ò  $¨ Ñ0Ô0Ð0Ý˜ Ñ)Ô)Ð)Ð)Ð)ð	*øøøð
 ˆs   �; »6A4Á3A4c                 ó|  — |                       ¦   «          | j        rp|                      ¦   «         s
J d¦   «         ‚| j                             ¦   «         }|                      |¦  «         t          ||                      ¦   «         ¦  «         dS |                      ¦   «         rt          ‚|                      |¦  «         dS )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_expiredrJ   rV   ÚpopleftÚ_Queue__put_internalr   Ú_getrX   r   )r;   rY   Úgetters      r   r[   zQueue.put_nowaitÑ   s±   € ð
 	×ÒÑÔÐØŒ=ð 	&Ø—:’:‘<”<ÐLÐLÐ!LÑLÔLÐLØ”]×*Ò*Ñ,Ô,ˆFØ×Ò Ñ%Ô%Ð%Ý.¨v°t·y²y±{´{ÑCÔCÐCÐCÐCØ�YŠY‰[Œ[ð 	&ÝˆOà×Ò Ñ%Ô%Ð%Ð%Ð%r   c                 óè   — t          ¦   «         }	 |                     |                      ¦   «         ¦  «         n:# t          $ r- | j                             |¦  «         t          ||¦  «         Y nw xY w|S )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   rJ   r]   r5   )r;   r!   r    s      r   r>   z	Queue.getá   s�   € õ. ‘”ˆð	*Ø×Ò˜dŸošoÑ/Ô/Ñ0Ô0Ð0Ð0øÝð 	*ð 	*ð 	*ØŒM× Ò  Ñ(Ô(Ð(Ý˜ Ñ)Ô)Ð)Ð)Ð)ð	*øøøð ˆs   �'8 ¸4A/Á.A/c                 ó|  — |                       ¦   «          | j        rs|                      ¦   «         s
J d¦   «         ‚| j                             ¦   «         \  }}|                      |¦  «         t          |d¦  «         |                      ¦   «         S |                      ¦   «         r|                      ¦   «         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`   rK   rX   ra   rb   r   rc   rS   r   )r;   rY   Úputters      r   rf   zQueue.get_nowait   sª   € ð 	×ÒÑÔÐØŒ=ð 		Ø—9’9‘;”;ÐJÐJÐ JÑJÔJÐJØœ=×0Ò0Ñ2Ô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                             ¦   «          dS 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 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)rL   rE   rM   rN   r?   s    r   Ú	task_donezQueue.task_done  se   € ð Ô! QÒ&Ð&ÝÐ@ÑAÔAÐAØÐÔ !Ñ#ÐÔØÔ! QÒ&Ð&ØŒN×ÒÑ Ô Ð Ð Ð ð 'Ð&r   c                 ó6   — | j                              |¦  «        S )z›Block until all items in the queue are processed.

        Returns an awaitable, which raises `tornado.util.TimeoutError` after a
        timeout.
        )rM   Ú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        ¦   «         | _        d S r%   )rH   rI   rR   r?   s    r   rG   zQueue._init2  s   € Ý!Ô'Ñ)Ô)ˆŒˆˆr   c                 ó4   — | j                              ¦   «         S r%   )rR   ra   r?   s    r   rc   z
Queue._get5  s   € ØŒ{×"Ò"Ñ$Ô$Ð$r   c                 ó:   — | j                              |¦  «         d S r%   ©rR   r]   ©r;   rY   s     r   Ú_putz
Queue._put8  ó   € ØŒ×Ò˜4Ñ Ô Ð Ð Ð r   c                 ó‚   — | xj         dz  c_         | j                             ¦   «          |                      |¦  «         d S )Nrj   )rL   rM   Úclearrv   ru   s     r   Ú__put_internalzQueue.__put_internal=  s=   € ØÐÔ !Ñ#ÐÔØŒ×ÒÑÔÐØ�	Š	�$‰Œˆˆˆr   c                 ó¾  — | j         rj| j         d         d                              ¦   «         rE| j                              ¦   «          | j         r%| j         d         d                              ¦   «         °E| j        r`| j        d                              ¦   «         rE| j                             ¦   «          | j        r#| j        d                              ¦   «         °?d S d S d S d S )Nr   rj   )rK   r&   ra   rJ   r?   s    r   r`   zQueue._consume_expiredB  sò   € àŒmð 	$ ¤¨aÔ 0°Ô 3× 8Ò 8Ñ :Ô :ð 	$ØŒM×!Ò!Ñ#Ô#Ð#ð Œmð 	$ ¤¨aÔ 0°Ô 3× 8Ò 8Ñ :Ô :ð 	$ð Œmð 	$ ¤¨aÔ 0× 5Ò 5Ñ 7Ô 7ð 	$ØŒM×!Ò!Ñ#Ô#Ð#ð Œmð 	$ ¤¨aÔ 0× 5Ò 5Ñ 7Ô 7ð 	$ð 	$ð 	$ð 	$ð 	$ð 	$ð 	$ð 	$ð 	$r   c                 ó–   — dt          | ¦  «        j        › dt          t          | ¦  «        ¦  «        › d|                      ¦   «         › d�S )Nú<z at ú ú>)Útyper   ÚhexÚidÚ_formatr?   s    r   Ú__repr__zQueue.__repr__J  s@   € ØM•4˜‘:”:Ô&ÐMÐM­Cµ°4±´©M¬MÐMÐM¸D¿LºL¹N¼NÐMÐMÐMÐMr   c                 ó\   — dt          | ¦  «        j        › d|                      ¦   «         › d�S )Nr}   r~   r   )r€   r   rƒ   r?   s    r   Ú__str__zQueue.__str__M  s,   € Ø:•4˜‘:”:Ô&Ð:Ð:¨¯ª©¬Ð:Ð:Ð:Ð: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=rR   z	 queue=%rz getters[%s]z putters[%s]z	 tasks=%s)rB   ÚgetattrrR   rJ   rQ   rK   rL   )r;   Úresults     r   rƒ   zQueue._formatP  sž   € Ø,˜DœLÐ,Ð,ˆÝ�4˜ 4Ñ(Ô(ð 	0Ø�k D¤KÑ/Ñ/ˆFØŒ=ð 	:Ø�n¥s¨4¬=Ñ'9Ô'9Ñ9Ñ9ˆFØŒ=ð 	:Ø�n¥s¨4¬=Ñ'9Ô'9Ñ9Ñ9ˆFØÔ!ð 	;Ø�k DÔ$:Ñ:Ñ:ˆFØˆr   )r   r%   r0   )%r   r   r   r   rR   Úintr<   ÚpropertyrB   rS   ÚboolrV   rX   r   r   r   ÚfloatÚdatetimeÚ	timedeltar^   r[   r   r>   rf   rk   rn   r7   rp   rG   rc   rv   rb   r`   Ústrr„   r†   rƒ   r   r   r   r   r   Q   së  € € € € € ðAð AðJ €Fðð  ð ¨Dð ð ð ð ð ð˜ð ð ð ñ „Xðð �sð  ð  ð  ð  ð�tð ð ð ð ð0�dð 0ð 0ð 0ð 0ð OSðð ØðØ!)¨%°°xÔ7IÐ0IÔ*JÔ!Kðà	ðð ð ð ð.&˜rð & dð &ð &ð &ð &ð" EIðð Ø  e¨XÔ-?Ð&?Ô @ÔAðà	�2Œðð ð ð ð>˜Bð ð ð ð ð$!ð !ð !ð !ð& EIð,ð ,Ø  e¨XÔ-?Ð&?Ô @ÔAð,à	�4Œð,ð ,ð ,ð ,ð$˜>¨"Ô-ð $ð $ð $ð $ð*ð *ð *ð *ð%�bð %ð %ð %ð %ð!˜ð ! ð !ð !ð !ð !ð
 2ð ¨$ð ð ð ð ð
$ð $ð $ð $ðN˜#ð Nð Nð Nð Nð;˜ð ;ð ;ð ;ð ;ð
˜ð 
ð 
ð 
ð 
ð 
ð 
r   r   c                   ó6   — e Zd ZdZdd„Zdeddfd„Zdefd„ZdS )	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 | _         d S r%   rU   r?   s    r   rG   zPriorityQueue._initz  ó   € ØˆŒˆˆr   rY   c                 ó:   — t          j        | j        |¦  «         d S r%   )ÚheapqÚheappushrR   ru   s     r   rv   zPriorityQueue._put}  s   € ÝŒ�t”{ DÑ)Ô)Ð)Ð)Ð)r   c                 ó4   — t          j        | j        ¦  «        S r%   )r•   ÚheappoprR   r?   s    r   rc   zPriorityQueue._get€  s   € ÝŒ}˜Tœ[Ñ)Ô)Ð)r   r0   ©r   r   r   r   rG   r   rv   rc   r   r   r   r   r   ]  so   € € € € € ðð ð8ð ð ð ð*˜ð * ð *ð *ð *ð *ð*�bð *ð *ð *ð *ð *ð *r   r   c                   ó6   — e Zd ZdZdd„Zdeddfd„Zdefd„ZdS )	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 | _         d S r%   rU   r?   s    r   rG   zLifoQueue._initŸ  r“   r   rY   c                 ó:   — | j                              |¦  «         d S r%   rt   ru   s     r   rv   zLifoQueue._put¢  rw   r   c                 ó4   — | j                              ¦   «         S r%   )rR   Úpopr?   s    r   rc   zLifoQueue._get¥  s   € ØŒ{�ŠÑ Ô Ð r   r0   r™   r   r   r   r   r   „  so   € € € € € ðð ð4ð ð ð ð!˜ð ! ð !ð !ð !ð !ð!�bð !ð !ð !ð !ð !ð !r   r   )"r   rH   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Ð IÐ IÐ IÐ IÐ IÐ IÐ IØ Ð Ð Ð Ð Ð à ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø €€€à	Ôð )Ø(Ð(Ð(Ð(Ð(Ð(Ð(Ð(Ð(Ð(à€WˆT�]„]€à
LÐ
LÐ
L€ð	ð 	ð 	ð 	ð 	�ñ 	ô 	ð 	ð	ð 	ð 	ð 	ð 	�	ñ 	ô 	ð 	ðSØðSØ" 4¨°Ô0BÐ#BÔCðSà	ðSð Sð Sð Sðð ð ð ð �W˜R”[ñ ô ð ðIð Ið Ið Ið IˆG�BŒKñ Iô Ið IðX$*ð $*ð $*ð $*ð $*�Eñ $*ô $*ð $*ðN"!ð "!ð "!ð "!ð "!�ñ "!ô "!ð "!ð "!ð "!r   