o
    g'                     @  s4  d Z ddlmZ ddlZddlmZ ddlZddlmZ ddl	Z	ddl	m
Z
 ddl	mZ ddl	mZ dd	l	mZ dd
l	mZ ddl	mZ ddlmZ ddlmZ ddlmZ ede
dZg dZG dd deZG dd deZG dd dee ZG dd dee ZG dd dee ZG dd dee ZdS )a  An adaptation of Py2.3/2.4's Queue module which supports reentrant
behavior, using RLock instead of Lock for its mutex object.  The
Queue object is used exclusively by the sqlalchemy.pool.QueuePool
class.

This is to support the connection pool's usage of weakref callbacks to return
connections to the underlying Queue, which can in extremely
rare cases be invoked within the ``get()`` method of the Queue itself,
producing a ``put()`` inside the ``get()`` and therefore a reentrant
condition.

    )annotationsN)deque)time)Any)	Awaitable)Deque)Generic)Optional)TypeVar   )await_fallback)
await_only)memoized_property_T)bound)EmptyFullQueuec                   @     e Zd ZdZdS )r   z4Exception raised by Queue.get(block=0)/get_nowait().N__name__
__module____qualname____doc__ r   r   W/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/util/queue.pyr   ,       r   c                   @  r   )r   z4Exception raised by Queue.put(block=0)/put_nowait().Nr   r   r   r   r   r   2   r   r   c                   @  sv   e Zd ZU ded< ded< d d!ddZd"d
dZd"ddZd#ddZd$ddZ	d%d&ddZ	d'ddZ
d%d(ddZdS ))QueueCommonintmaxsizebooluse_lifor   Fc                 C     d S Nr   selfr   r!   r   r   r   __init__<   s    zQueueCommon.__init__returnc                 C     t  r#   NotImplementedErrorr%   r   r   r   empty>      zQueueCommon.emptyc                 C  r(   r#   r)   r+   r   r   r   fullA   r-   zQueueCommon.fullc                 C  r(   r#   r)   r+   r   r   r   qsizeD   r-   zQueueCommon.qsizeitemr   Nonec                 C  r(   r#   r)   r%   r0   r   r   r   
put_nowaitG   r-   zQueueCommon.put_nowaitTNblocktimeoutOptional[float]c                 C  r(   r#   r)   )r%   r0   r4   r5   r   r   r   putJ   s   zQueueCommon.putc                 C  r(   r#   r)   r+   r   r   r   
get_nowaitO   r-   zQueueCommon.get_nowaitc                 C  r(   r#   r)   )r%   r4   r5   r   r   r   getR   r-   zQueueCommon.getr   Fr   r   r!   r    r'   r    r'   r   r0   r   r'   r1   TNr0   r   r4   r    r5   r6   r'   r1   r'   r   r4   r    r5   r6   r'   r   )r   r   r   __annotations__r&   r,   r.   r/   r3   r7   r8   r9   r   r   r   r   r   8   s   
 




r   c                   @  s   e Zd ZU ded< d.d/d	d
Zd0ddZd1ddZd1ddZ	d2d3ddZd4ddZ	d2d5ddZ
d6d d!Zd7d"d#Zd0d$d%Zd1d&d'Zd1d(d)Zd4d*d+Zd6d,d-ZdS )8r   z	Deque[_T]queuer   Fr   r   r!   r    c                 C  s:   |  | t | _t| j| _t| j| _|| _dS )zInitialize a queue object with a given maximum size.

        If `maxsize` is <= 0, the queue size is infinite.

        If `use_lifo` is True, this Queue acts like a Stack (LIFO).
        N)_init	threadingRLockmutex	Condition	not_emptynot_fullr!   r$   r   r   r   r&   Y   s
   


zQueue.__init__r'   c                 C  2   | j  |  W  d   S 1 sw   Y  dS )z9Return the approximate size of the queue (not reliable!).N)rH   _qsizer+   r   r   r   r/   p   s   $zQueue.qsizec                 C  rL   )zKReturn True if the queue is empty, False otherwise (not
        reliable!).N)rH   _emptyr+   r   r   r   r,   v      $zQueue.emptyc                 C  rL   )zJReturn True if the queue is full, False otherwise (not
        reliable!).N)rH   _fullr+   r   r   r   r.   }   rO   z
Queue.fullTNr0   r   r4   r5   r6   r1   c                 C  s   | j T |s|  rtn8|du r|  r| j   |  sn&|dk r'tdt | }|  rE|t  }|dkr;t| j | |  s0| | | j  W d   dS 1 sZw   Y  dS )a  Put an item into the queue.

        If optional args `block` is True and `timeout` is None (the
        default), block if necessary until a free slot is
        available. If `timeout` is a positive number, it blocks at
        most `timeout` seconds and raises the ``Full`` exception if no
        free slot was available within that time.  Otherwise (`block`
        is false), put an item on the queue if a free slot is
        immediately available, else raise the ``Full`` exception
        (`timeout` is ignored in that case).
        Nr   #'timeout' must be a positive number        )	rK   rP   r   wait
ValueError_time_putrJ   notify)r%   r0   r4   r5   endtime	remainingr   r   r   r7      s,   



"z	Queue.putc                 C  s   |  |dS )zPut an item into the queue without blocking.

        Only enqueue the item if a free slot is immediately available.
        Otherwise raise the ``Full`` exception.
        F)r7   r2   r   r   r   r3      s   zQueue.put_nowaitc                 C  s   | j T |s|  rtn8|du r|  r| j   |  sn&|dk r'tdt | }|  rE|t  }|dkr;t| j | |  s0|  }| j  |W  d   S 1 sZw   Y  dS )a  Remove and return an item from the queue.

        If optional args `block` is True and `timeout` is None (the
        default), block if necessary until an item is available. If
        `timeout` is a positive number, it blocks at most `timeout`
        seconds and raises the ``Empty`` exception if no item was
        available within that time.  Otherwise (`block` is false),
        return an item if one is immediately available, else raise the
        ``Empty`` exception (`timeout` is ignored in that case).

        Nr   rQ   rR   )	rJ   rN   r   rS   rT   rU   _getrK   rW   )r%   r4   r5   rX   rY   r0   r   r   r   r9      s.   



$z	Queue.getc                 C  s
   |  dS )zRemove and return an item from the queue without blocking.

        Only get an item if one is immediately available. Otherwise
        raise the ``Empty`` exception.
        F)r9   r+   r   r   r   r8      s   
zQueue.get_nowaitc                 C  s   || _ t | _d S r#   )r   r   rD   )r%   r   r   r   r   rE      s   zQueue._initc                 C  s
   t | jS r#   )lenrD   r+   r   r   r   rM         
zQueue._qsizec                 C  s   | j  S r#   )rD   r+   r   r   r   rN      s   zQueue._emptyc                 C  s   | j dkot| j| j kS )Nr   )r   r[   rD   r+   r   r   r   rP      s   zQueue._fullc                 C  s   | j | d S r#   )rD   appendr2   r   r   r   rV      s   z
Queue._putc                 C  s   | j r| j S | j S r#   )r!   rD   poppopleftr+   r   r   r   rZ      s   

z
Queue._getr:   r;   r=   r<   r?   r@   r>   rB   rA   )r   r   r'   r1   )r   r   r   rC   r&   r/   r,   r.   r7   r3   r9   r8   rE   rM   rN   rP   rV   rZ   r   r   r   r   r   V   s"   
 



"
 
	



r   c                   @  s   e Zd Zejred'ddZneeZd(d)ddZd*ddZ	dd Z
dd Zed+ddZd,ddZ	d-d.d!d"Zd/d#d$Zd-d0d%d&ZdS )1AsyncAdaptedQueue	coroutineAwaitable[Any]r'   r   c                 C  r"   r#   r   )ra   r   r   r   await_   s   zAsyncAdaptedQueue.await_r   Fr   r   r!   r    c                 C  s   || _ || _d S r#   )r!   r   r$   r   r   r   r&      s   
zAsyncAdaptedQueue.__init__c                 C  
   | j  S r#   )_queuer,   r+   r   r   r   r,      r\   zAsyncAdaptedQueue.emptyc                 C  rd   r#   )re   r.   r+   r   r   r   r.      r\   zAsyncAdaptedQueue.fullc                 C  rd   r#   )re   r/   r+   r   r   r   r/     r\   zAsyncAdaptedQueue.qsizeasyncio.Queue[_T]c                 C  s*   | j rtj| jd}|S tj| jd}|S )N)r   )r!   asyncio	LifoQueuer   r   )r%   rD   r   r   r   re     s
   zAsyncAdaptedQueue._queuer0   r1   c              
   C  s6   z	| j | W d S  tjy } zt |d }~ww r#   )re   r3   rg   	QueueFullr   )r%   r0   errr   r   r   r3     s   zAsyncAdaptedQueue.put_nowaitTNr4   r5   r6   c              
   C  sx   |s|  |S z |d ur| t| j|| W d S | | j| W d S  tjtjfy; } zt |d }~ww r#   )	r3   rc   rg   wait_forre   r7   ri   TimeoutErrorr   )r%   r0   r4   r5   rj   r   r   r   r7     s   
 zAsyncAdaptedQueue.putc              
   C  s0   z| j  W S  tjy } zt |d }~ww r#   )re   r8   rg   
QueueEmptyr   )r%   rj   r   r   r   r8   +  s   zAsyncAdaptedQueue.get_nowaitc              
   C  sj   |s|   S z|d ur| t| j |W S | | j W S  tjtjfy4 } zt |d }~ww r#   )	r8   rc   rg   rk   re   r9   rm   rl   r   )r%   r4   r5   rj   r   r   r   r9   1  s   zAsyncAdaptedQueue.get)ra   rb   r'   r   r:   r;   r<   )r'   rf   r>   r?   r@   rA   rB   )r   r   r   typingTYPE_CHECKINGstaticmethodrc   r   r&   r,   r.   r/   r   re   r3   r7   r8   r9   r   r   r   r   r`      s     


r`   c                   @  s   e Zd ZejseeZdS dS )FallbackAsyncAdaptedQueueN)r   r   r   rn   ro   rp   r   rc   r   r   r   r   rq   @  s    rq   )r   
__future__r   rg   collectionsr   rF   r   rU   rn   r   r   r   r   r	   r
   concurrencyr   r   langhelpersr   r   __all__	Exceptionr   r   r   r   r`   rq   r   r   r   r   <module>   s2    P