a
    õ£Beò0  ã                   @   s   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„ deƒZG dd„ deƒZeedeejf ddœdd„ZG dd„ dee ƒZG dd„ dee ƒZG dd„ deƒZ G d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                   @   s   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__© r   r   úQ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tornado/queues.pyr   /   s   r   c                   @   s   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   r   )ÚfutureÚtimeoutÚreturnc                    sD   |r@d dœ‡ fdd„}t j ¡ ‰ˆ ||¡‰ˆ  ‡‡fdd„¡ d S )N©r   c                      s   ˆ   ¡ sˆ  t ¡ ¡ d S ©N)ÚdoneÚset_exceptionr   ÚTimeoutErrorr   )r   r   r   Ú
on_timeout@   s    z _set_timeout.<locals>.on_timeoutc                    s
   ˆ   ˆ¡S r    )Zremove_timeout)Ú_)Úio_loopÚtimeout_handler   r   Ú<lambda>F   ó    z_set_timeout.<locals>.<lambda>)r   ZIOLoopÚcurrentZadd_timeoutÚadd_done_callback)r   r   r$   r   )r   r&   r'   r   Ú_set_timeout;   s
    
r,   c                   @   s.   e Zd Zdddœdd„Zee dœdd„ZdS )	Ú_QueueIteratorz	Queue[_T]N)Úqr   c                 C   s
   || _ d S r    )r.   )Úselfr.   r   r   r   Ú__init__J   s    z_QueueIterator.__init__r   c                 C   s
   | j  ¡ S r    )r.   Úget©r/   r   r   r   Ú	__anext__M   s    z_QueueIterator.__anext__)r   r   r   r0   r
   r   r3   r   r   r   r   r-   I   s   r-   c                   @   s~  e Zd ZdZdZd2eddœdd„Zeedœdd	„ƒZedœd
d„Z	e
dœdd„Ze
dœdd„Zd3eeeeejf  ddœdd„Zeddœdd„Zd4eeeejf  ee dœdd„Zedœdd„Zddœdd„Zd5eeeejf  ed dœdd„Zee dœd d!„Zddœd"d#„Zedœd$d%„Zeddœd&d'„Zeddœd(d)„Zddœd*d+„Z e!dœd,d-„Z"e!dœd.d/„Z#e!dœd0d1„Z$dS )6r   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                 C   sb   |d u rt dƒ‚|dk r tdƒ‚|| _|  ¡  t g ¡| _t 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/   r4   r   r   r   r0   ™   s    zQueue.__init__r   c                 C   s   | j S )z%Number of items allowed in the queue.)r7   r2   r   r   r   r4   ¨   s    zQueue.maxsizec                 C   s
   t | jƒS )zNumber of items in the queue.)ÚlenÚ_queuer2   r   r   r   Úqsize­   s    zQueue.qsizec                 C   s   | j  S r    ©rA   r2   r   r   r   Úempty±   s    zQueue.emptyc                 C   s    | j dkrdS |  ¡ | j kS d S )Nr   F)r4   rB   r2   r   r   r   Úfull´   s    
z
Queue.fullzFuture[None])Úitemr   r   c                 C   sP   t ƒ }z|  |¡ W n, ty@   | j ||f¡ t||ƒ Y n0 | d¡ |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_nowaitr   r<   Úappendr,   Ú
set_result)r/   rF   r   r   r   r   r   Úputº   s    
z	Queue.put©rF   r   c                 C   s^   |   ¡  | jrB|  ¡ sJ dƒ‚| j ¡ }|  |¡ t||  ¡ ƒ n|  ¡ rPt‚n
|  |¡ 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_expiredr;   rD   ÚpopleftÚ_Queue__put_internalr   Ú_getrE   r   )r/   rF   Úgetterr   r   r   rG   Ñ   s    

zQueue.put_nowait)r   r   c                 C   sF   t ƒ }z| |  ¡ ¡ W n( ty@   | j |¡ t||ƒ Y n0 |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   rI   Ú
get_nowaitr   r;   rH   r,   )r/   r   r   r   r   r   r1   á   s    z	Queue.getc                 C   s`   |   ¡  | jrH|  ¡ sJ dƒ‚| j ¡ \}}|  |¡ t|dƒ |  ¡ S |  ¡ rX|  ¡ S t‚dS )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)	rL   r<   rE   rM   rN   r   rO   rB   r   )r/   rF   Úputterr   r   r   rQ      s    

zQueue.get_nowaitc                 C   s8   | j dkrtdƒ‚|  j d8  _ | j dkr4| j ¡  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)r=   r6   r>   r?   r2   r   r   r   Ú	task_done  s
    

zQueue.task_donec                 C   s   | j  |¡S )z›Block until all items in the queue are processed.

        Returns an awaitable, which raises `tornado.util.TimeoutError` after a
        timeout.
        )r>   Úwait)r/   r   r   r   r   Újoin$  s    z
Queue.joinc                 C   s   t | ƒS r    )r-   r2   r   r   r   Ú	__aiter__.  s    zQueue.__aiter__c                 C   s   t  ¡ | _d S r    )r9   r:   rA   r2   r   r   r   r8   2  s    zQueue._initc                 C   s
   | j  ¡ S r    )rA   rM   r2   r   r   r   rO   5  s    z
Queue._getc                 C   s   | j  |¡ d S r    ©rA   rH   ©r/   rF   r   r   r   Ú_put8  s    z
Queue._putc                 C   s&   |  j d7  _ | j ¡  |  |¡ d S )NrS   )r=   r>   ÚclearrZ   rY   r   r   r   Z__put_internal=  s    
zQueue.__put_internalc                 C   sH   | j r$| j d d  ¡ r$| j  ¡  q | jrD| jd  ¡ rD| j ¡  q$d S )Nr   rS   )r<   r!   rM   r;   r2   r   r   r   rL   B  s    zQueue._consume_expiredc                 C   s    dt | ƒjtt| ƒƒ|  ¡ f S )Nz<%s at %s %s>)Útyper   ÚhexÚidÚ_formatr2   r   r   r   Ú__repr__J  s    zQueue.__repr__c                 C   s   dt | ƒj|  ¡ f S )Nz<%s %s>)r\   r   r_   r2   r   r   r   Ú__str__M  s    zQueue.__str__c                 C   sn   d| j f }t| dd ƒr&|d| j 7 }| jr>|dt| jƒ 7 }| jrV|dt| jƒ 7 }| jrj|d| j 7 }|S )Nz
maxsize=%rrA   z	 queue=%rz getters[%s]z putters[%s]z	 tasks=%s)r4   ÚgetattrrA   r;   r@   r<   r=   )r/   Úresultr   r   r   r_   P  s    zQueue._format)r   )N)N)N)%r   r   r   r   rA   Úintr0   Úpropertyr4   rB   ÚboolrD   rE   r   r   r   ÚfloatÚdatetimeÚ	timedeltarJ   rG   r
   r1   rQ   rT   rV   r-   rW   r8   rO   rZ   rN   rL   Ústrr`   ra   r_   r   r   r   r   r   Q   sF   E ÿþ ÿþ ÿþ
r   c                   @   s<   e Zd ZdZddœdd„Zeddœdd„Zedœd	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')
    Nr   c                 C   s
   g | _ d S r    rC   r2   r   r   r   r8   z  s    zPriorityQueue._initrK   c                 C   s   t  | j|¡ d S r    )ÚheapqÚheappushrA   rY   r   r   r   rZ   }  s    zPriorityQueue._putc                 C   s   t  | j¡S r    )rk   ÚheappoprA   r2   r   r   r   rO   €  s    zPriorityQueue._get©r   r   r   r   r8   r   rZ   rO   r   r   r   r   r   ]  s   r   c                   @   s<   e Zd ZdZddœdd„Zeddœdd„Zedœd	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
    Nr   c                 C   s
   g | _ d S r    rC   r2   r   r   r   r8   Ÿ  s    zLifoQueue._initrK   c                 C   s   | j  |¡ d S r    rX   rY   r   r   r   rZ   ¢  s    zLifoQueue._putc                 C   s
   | j  ¡ S r    )rA   Úpopr2   r   r   r   rO   ¥  s    zLifoQueue._getrn   r   r   r   r   r   „  s   r   )"r   r9   rh   rk   Útornador   r   Ztornado.concurrentr   r   Ztornado.locksr   Útypingr   r   r	   r
   r   ÚTYPE_CHECKINGr   r   r   r   Ú__all__Ú	Exceptionr   r   rg   ri   r,   r-   r   r   r   r   r   r   r   Ú<module>   s.   þ  '