
    h2                        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       aC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-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)_QueueIteratorI   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    vvzz|r    r@   )rA   z	Queue[_T]r&   N)	r   r   r   r   rC   r   r   rH   r   r   r    r!   r=   r=   I   s    9R= 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     	$OOD!
 d#  	*MM  $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%.vyy{CYY[[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\    . 	*doo/0   	*MM  ()	*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)zRemove 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     	==99;;J JJ;==002LD%.vt<99;ZZ\\99;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&NN  'r    c                 8    U R                   R                  U5      $ )zBlock 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    mma 0 3 8 8 : :MM!!# mma 0 3 8 8 : : mma 0 5 5 7 7MM!!# mma 0 5 5 7 7m 7mr    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  s7    4:&&'tC4M?!DLLN;K1MMr    c                 V    S[        U 5      R                   SU R                  5        S3$ )Nr   r   r   )r   r   r   rG   s    r!   __str__Queue.__str__M  s)    4:&&'q(8::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    DLL+,44((kDKK//F==ns4=='999F==ns4=='999F!!kD$:$:::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 0d 0 OS!)%x7I7I0I*J!K	.&r &d &" EIeX-?-?&? @A	2>B $!& EI,eX-?-?&? @A,	4,$>"- $*%b %! ! !
2 $ 
$N# N; ;
 
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0B0B#BCS	SWR[ IGBK IX$*E $*N"! "!r    