o
    g J                     @  s  d Z ddlmZ ddlZddl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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 ddlmZ ddlmZ ddlmZ ddlmZ ejrddl m!Z! G dd deZ"G dd de"Z#G d d! d!e#Z$G d"d# d#eZ%G d$d% d%eZ&G d&d' d'eZ'G d(d) d)eZ(dS )*zPool implementation classes.

    )annotationsN)Any)cast)List)Optional)Set)Type)TYPE_CHECKING)Union   )_AsyncConnDialect)_ConnectionFairy_ConnectionRecord)_CreatorFnType)_CreatorWRecFnType)ConnectionPoolEntry)Pool)PoolProxiedConnection   )exc)util)chop_traceback)queue)Literal)DBAPIConnectionc                   @  s   e Zd ZU dZdZejZded< ded< 					d6d7ddZ	d8ddZ
d9ddZd:dd Zd;d"d#Zd<d$d%Zd=d&d'Zd>d)d*Zd?d+d,Zd@d-d.Zd?d/d0Zd?d1d2Zd?d3d4Zd5S )A	QueuePoolaO  A :class:`_pool.Pool`
    that imposes a limit on the number of open connections.

    :class:`.QueuePool` is the default pooling implementation used for
    all :class:`_engine.Engine` objects other than SQLite with a ``:memory:``
    database.

    The :class:`.QueuePool` class **is not compatible** with asyncio and
    :func:`_asyncio.create_async_engine`.  The
    :class:`.AsyncAdaptedQueuePool` class is used automatically when
    using :func:`_asyncio.create_async_engine`, if no other kind of pool
    is specified.

    .. seealso::

        :class:`.AsyncAdaptedQueuePool`

    F1Type[sqla_queue.QueueCommon[ConnectionPoolEntry]]_queue_classz+sqla_queue.QueueCommon[ConnectionPoolEntry]_pool   
         >@creator)Union[_CreatorFnType, _CreatorWRecFnType]	pool_sizeintmax_overflowtimeoutfloatuse_lifoboolkwr   c                 K  sT   t j| |fi | | j||d| _d| | _|dkrdn|| _|| _t | _	dS )a	  
        Construct a QueuePool.

        :param creator: a callable function that returns a DB-API
          connection object, same as that of :paramref:`_pool.Pool.creator`.

        :param pool_size: The size of the pool to be maintained,
          defaults to 5. This is the largest number of connections that
          will be kept persistently in the pool. Note that the pool
          begins with no connections; once this number of connections
          is requested, that number of connections will remain.
          ``pool_size`` can be set to 0 to indicate no size limit; to
          disable pooling, use a :class:`~sqlalchemy.pool.NullPool`
          instead.

        :param max_overflow: The maximum overflow size of the
          pool. When the number of checked-out connections reaches the
          size set in pool_size, additional connections will be
          returned up to this limit. When those additional connections
          are returned to the pool, they are disconnected and
          discarded. It follows then that the total number of
          simultaneous connections the pool will allow is pool_size +
          `max_overflow`, and the total number of "sleeping"
          connections the pool will allow is pool_size. `max_overflow`
          can be set to -1 to indicate no overflow limit; no limit
          will be placed on the total number of concurrent
          connections. Defaults to 10.

        :param timeout: The number of seconds to wait before giving up
          on returning a connection. Defaults to 30.0. This can be a float
          but is subject to the limitations of Python time functions which
          may not be reliable in the tens of milliseconds.

        :param use_lifo: use LIFO (last-in-first-out) when retrieving
          connections instead of FIFO (first-in-first-out). Using LIFO, a
          server-side timeout scheme can reduce the number of connections used
          during non-peak periods of use.   When planning for server-side
          timeouts, ensure that a recycle or pre-ping strategy is in use to
          gracefully handle stale connections.

          .. versionadded:: 1.3

          .. seealso::

            :ref:`pool_use_lifo`

            :ref:`pool_disconnects`

        :param \**kw: Other keyword arguments including
          :paramref:`_pool.Pool.recycle`, :paramref:`_pool.Pool.echo`,
          :paramref:`_pool.Pool.reset_on_return` and others are passed to the
          :class:`_pool.Pool` constructor.

        )r*   r   N)
r   __init__r   r   	_overflow_max_overflow_timeout	threadingLock_overflow_lock)selfr#   r%   r'   r(   r*   r,    r6   V/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/pool/impl.pyr.   I   s   @
zQueuePool.__init__recordr   returnNonec                 C  sJ   z
| j |d W d S  tjy$   z|  W |   Y d S |   w w NF)r   put
sqla_queueFullclose_dec_overflowr5   r8   r6   r6   r7   _do_return_conn   s   
zQueuePool._do_return_connc                 C  s   | j dk}|o| j| j k}z	| j|| jW S  tjy    Y nw |r@| j| j kr@|s/|  S tj	d| 
 |  | jf dd|  rfz|  W S    t  |   W d     1 s`w   Y   |  S )Nr-   zSQueuePool limit of size %d overflow %d reached, connection timed out, timeout %0.2f3o7r)code)r0   r/   r   getr1   r=   Empty_do_getr   TimeoutErrorsizeoverflow_inc_overflow_create_connectionr   safe_reraiser@   )r5   use_overflowwaitr6   r6   r7   rG      s6   




zQueuePool._do_getc                 C  sv   | j dkr|  jd7  _dS | j  | j| j k r(|  jd7  _	 W d    dS 	 W d    dS 1 s4w   Y  d S )Nr-   r   TFr0   r/   r4   r5   r6   r6   r7   rK      s   
$zQueuePool._inc_overflowLiteral[True]c                 C  sX   | j dkr|  jd8  _dS | j |  jd8  _	 W d    dS 1 s%w   Y  d S )Nr-   r   TrP   rQ   r6   r6   r7   r@      s   
$zQueuePool._dec_overflowc                 C  sJ   | j d | j| j| jj| j| j| jj| j	| j
| j| j| j| j| jdS )NPool recreating)r%   r'   pre_pingr*   r(   recycleechologging_namereset_on_return	_dispatchdialect)loggerinfo	__class___creatorr   maxsizer0   	_pre_pingr*   r1   _recyclerV   _orig_logging_name_reset_on_returndispatch_dialectrQ   r6   r6   r7   recreate   s   zQueuePool.recreatec                 C  sV   	 z| j d}|  W n
 tjy   Y nw qd|   | _| jd| 	  d S )NTFr   zPool disposed. %s)
r   rE   r?   r=   rF   rI   r/   r[   r\   statusr5   connr6   r6   r7   dispose   s   zQueuePool.disposestrc                 C  s    d|   |  |  |  f S )Nz_Pool size: %d  Connections in pool: %d Current Overflow: %d Current Checked out connections: %d)rI   	checkedinrJ   
checkedoutrQ   r6   r6   r7   rg      s   zQueuePool.statusc                 C  s   | j jS N)r   r_   rQ   r6   r6   r7   rI         zQueuePool.sizec                 C  s   | j S rn   )r1   rQ   r6   r6   r7   r(         zQueuePool.timeoutc                 C  s
   | j  S rn   )r   qsizerQ   r6   r6   r7   rl      s   
zQueuePool.checkedinc                 C  s   | j jr| jS dS )Nr   )r   r_   r/   rQ   r6   r6   r7   rJ      s   zQueuePool.overflowc                 C  s   | j j| j   | j S rn   )r   r_   rq   r/   rQ   r6   r6   r7   rm     s   zQueuePool.checkedoutN)r    r!   r"   F)r#   r$   r%   r&   r'   r&   r(   r)   r*   r+   r,   r   r8   r   r9   r:   r9   r   )r9   r+   )r9   rR   )r9   r   r9   r:   r9   rk   )r9   r&   )r9   r)   )__name__
__module____qualname____doc___is_asyncior=   Queuer   __annotations__r.   rB   rG   rK   r@   rf   rj   rg   rI   r(   rl   rJ   rm   r6   r6   r6   r7   r   -   s.   
 

G
	
 







r   c                   @  s*   e Zd ZU dZdZejZded< e	 Z
dS )AsyncAdaptedQueuePoola  An asyncio-compatible version of :class:`.QueuePool`.

    This pool is used by default when using :class:`.AsyncEngine` engines that
    were generated from :func:`_asyncio.create_async_engine`.   It uses an
    asyncio-compatible queue implementation that does not use
    ``threading.Lock``.

    The arguments and operation of :class:`.AsyncAdaptedQueuePool` are
    otherwise identical to that of :class:`.QueuePool`.

    Tr   r   N)rv   rw   rx   ry   rz   r=   AsyncAdaptedQueuer   r|   r   re   r6   r6   r6   r7   r}     s   
 

r}   c                   @  s   e Zd ZejZdS )FallbackAsyncAdaptedQueuePoolN)rv   rw   rx   r=   FallbackAsyncAdaptedQueuer   r6   r6   r6   r7   r     s    
r   c                   @  sB   e Zd ZdZdddZdd	d
ZdddZdddZdddZdS )NullPoola  A Pool which does not pool connections.

    Instead it literally opens and closes the underlying DB-API connection
    per each connection open/close.

    Reconnect-related functions such as ``recycle`` and connection
    invalidation are not supported by this Pool implementation, since
    no connections are held persistently.

    The :class:`.NullPool` class **is compatible** with asyncio and
    :func:`_asyncio.create_async_engine`.

    r9   rk   c                 C     dS )Nr   r6   rQ   r6   r6   r7   rg   -     zNullPool.statusr8   r   r:   c                 C  s   |   d S rn   )r?   rA   r6   r6   r7   rB   0  s   zNullPool._do_return_connc                 C  s   |   S rn   )rL   rQ   r6   r6   r7   rG   3  ro   zNullPool._do_getc              
   C  6   | j d | j| j| j| j| j| j| j| j	| j
dS )NrS   )rU   rV   rW   rX   rT   rY   rZ   )r[   r\   r]   r^   ra   rV   rb   rc   r`   rd   re   rQ   r6   r6   r7   rf   6  s   zNullPool.recreatec                 C     d S rn   r6   rQ   r6   r6   r7   rj   D  r   zNullPool.disposeNru   rr   rs   )r9   r   rt   )	rv   rw   rx   ry   rg   rB   rG   rf   rj   r6   r6   r6   r7   r     s    



r   c                   @  sh   e Zd ZdZdZ	d!d"d
dZd#ddZd$ddZd$ddZd%ddZ	d&ddZ
d'ddZd(ddZd S ))SingletonThreadPoola'  A Pool that maintains one connection per thread.

    Maintains one connection per each thread, never moving a connection to a
    thread other than the one which it was created in.

    .. warning::  the :class:`.SingletonThreadPool` will call ``.close()``
       on arbitrary connections that exist beyond the size setting of
       ``pool_size``, e.g. if more unique **thread identities**
       than what ``pool_size`` states are used.   This cleanup is
       non-deterministic and not sensitive to whether or not the connections
       linked to those thread identities are currently in use.

       :class:`.SingletonThreadPool` may be improved in a future release,
       however in its current status it is generally used only for test
       scenarios using a SQLite ``:memory:`` database and is not recommended
       for production use.

    The :class:`.SingletonThreadPool` class **is not compatible** with asyncio
    and :func:`_asyncio.create_async_engine`.


    Options are the same as those of :class:`_pool.Pool`, as well as:

    :param pool_size: The number of threads in which to maintain connections
        at once.  Defaults to five.

    :class:`.SingletonThreadPool` is used by the SQLite dialect
    automatically when a memory-based database is used.
    See :ref:`sqlite_toplevel`.

    Fr    r#   r$   r%   r&   r,   r   c                 K  s:   t j| |fi | t | _t | _t | _|| _d S rn   )	r   r.   r2   local_conn_fairyset
_all_connsrI   )r5   r#   r%   r,   r6   r6   r7   r.   k  s
   


zSingletonThreadPool.__init__r9   c                 C  s:   | j d | j| j| j| j| j| j| j| j	| j
| jd	S )NrS   )r%   rU   rV   rT   rW   rX   rY   rZ   )r[   r\   r]   r^   rI   ra   rV   r`   rb   rc   rd   re   rQ   r6   r6   r7   rf   w  s   zSingletonThreadPool.recreater:   c              	   C  s8   | j D ]}z|  W q ty   Y qw | j   dS )zDispose of this pool.N)r   r?   	Exceptionclearrh   r6   r6   r7   rj     s   
zSingletonThreadPool.disposec                 C  s:   t | j| jkr| j }|  t | j| jksd S d S rn   )lenr   rI   popr?   r5   cr6   r6   r7   _cleanup  s   
zSingletonThreadPool._cleanuprk   c                 C  s   dt | t| jf S )Nz"SingletonThreadPool id:%d size: %d)idr   r   rQ   r6   r6   r7   rg     s   zSingletonThreadPool.statusr8   r   c                 C  s"   z| j `W d S  ty   Y d S w rn   )r   currentAttributeErrorrA   r6   r6   r7   rB     s
   z#SingletonThreadPool._do_return_connc                 C  s   zt rtt| j }n| j }|r|W S W n	 ty    Y nw |  }t|| j_t	| j
| jkr8|   | j
| |S rn   )r	   r   r   r   r   r   rL   weakrefrefr   r   rI   r   addr   r6   r6   r7   rG     s    
zSingletonThreadPool._do_getr   c                 C  sF   z
t t| j }W n	 ty   Y n	w |d ur| S t| | jS rn   )r   r   r   r   r   _checkout_existing	_checkoutr5   recr6   r6   r7   connect  s   zSingletonThreadPool.connectN)r    )r#   r$   r%   r&   r,   r   )r9   r   rt   ru   rr   rs   )r9   r   )rv   rw   rx   ry   rz   r.   rf   rj   r   rg   rB   rG   r   r6   r6   r6   r7   r   H  s     





r   c                   @  sf   e Zd ZdZejdddZdddZdd
dZdddZ	dddZ
dddZd ddZdddZdS )!
StaticPoola~  A Pool of exactly one connection, used for all requests.

    Reconnect-related functions such as ``recycle`` and connection
    invalidation (which is also used to support auto-reconnect) are only
    partially supported right now and may not yield good results.

    The :class:`.StaticPool` class **is compatible** with asyncio and
    :func:`_asyncio.create_async_engine`.

    r9   r   c                 C  s   t | S rn   r   rQ   r6   r6   r7   
connection  s   zStaticPool.connectionrk   c                 C  r   )Nr   r6   rQ   r6   r6   r7   rg     r   zStaticPool.statusr:   c                 C  s4   d| j v r| jjd ur| j  | j d= d S d S d S Nr   )__dict__r   dbapi_connectionr?   rQ   r6   r6   r7   rj     s   

zStaticPool.disposec              
   C  r   )NrS   )r#   rU   rX   rT   rV   rW   rY   rZ   )r[   r\   r]   r^   ra   rc   r`   rV   rb   rd   re   rQ   r6   r6   r7   rf        zStaticPool.recreateother_static_poolc                   s   d fdd}|| _ d S )Nr   r   r9   r   c                   s    j j}|d us
J |S rn   )r   r   )r   ri   r   r6   r7   r#     s   z*StaticPool._transfer_from.<locals>.creator)r   r   r9   r   )_invoke_creator)r5   r   r#   r6   r   r7   _transfer_from  s   
zStaticPool._transfer_fromr   c                 C  s   t  rn   )NotImplementedErrorrQ   r6   r6   r7   rL     rp   zStaticPool._create_connectionr8   c                 C  r   rn   r6   rA   r6   r6   r7   rB     r   zStaticPool._do_return_connc                 C  s    | j }| r| jd= | j }|S r   )r   _is_hard_or_soft_invalidatedr   r   r6   r6   r7   rG     s
   zStaticPool._do_getN)r9   r   ru   rt   )r9   r   )r   r   r9   r:   rs   rr   )rv   rw   rx   ry   r   memoized_propertyr   rg   rj   rf   r   rL   rB   rG   r6   r6   r6   r7   r     s    






r   c                   @  s^   e Zd ZU dZded< ded< dd	d
ZdddZdddZdddZdddZ	d ddZ
dS )!AssertionPoola}  A :class:`_pool.Pool` that allows at most one checked out connection at
    any given time.

    This will raise an exception if more than one connection is checked out
    at a time.  Useful for debugging code that is using more connections
    than desired.

    The :class:`.AssertionPool` class **is compatible** with asyncio and
    :func:`_asyncio.create_async_engine`.

    zOptional[ConnectionPoolEntry]r   zOptional[List[str]]_checkout_tracebackargsr   r,   c                 O  s<   d | _ d| _|dd| _d | _tj| g|R i | d S )NFstore_tracebackT)r   _checked_outr   _store_tracebackr   r   r.   )r5   r   r,   r6   r6   r7   r.     s
   zAssertionPool.__init__r9   rk   c                 C  r   )Nr   r6   rQ   r6   r6   r7   rg     r   zAssertionPool.statusr8   r   r:   c                 C  s&   | j stdd| _ || ju sJ d S )Nzconnection is not checked outF)r   AssertionErrorr   rA   r6   r6   r7   rB     s   zAssertionPool._do_return_connc                 C  s   d| _ | jr| j  d S d S r;   )r   r   r?   rQ   r6   r6   r7   rj   #  s   zAssertionPool.disposec              
   C  r   )NrS   )rV   rT   rU   rX   rW   rY   rZ   )r[   r\   r]   r^   rV   r`   ra   rc   rb   rd   re   rQ   r6   r6   r7   rf   (  r   zAssertionPool.recreatec                 C  s^   | j r| jrddt| j }nd}td| | js!|  | _d| _ | jr,t	 | _| jS )Nz at:
%s z!connection is already checked outT)
r   r   joinr   r   r   rL   r   	tracebackformat_stack)r5   suffixr6   r6   r7   rG   5  s   

zAssertionPool._do_getN)r   r   r,   r   ru   rr   rt   )r9   r   rs   )rv   rw   rx   ry   r|   r.   rg   rB   rj   rf   rG   r6   r6   r6   r7   r     s   
 




r   ))ry   
__future__r   r2   r   typingr   r   r   r   r   r   r	   r
   r   baser   r   r   r   r   r   r   r   r   r   r   r   r   r=   util.typingr   engine.interfacesr   r   r}   r   r   r   r   r   r6   r6   r6   r7   <module>   sJ    Y*zA