U a?@sdZddlZddlZddlmZddlmZddlmZddlmZdd lm Z dd lm Z dd l m Z dd l m Z dd l mZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZdS)zPool implementation classes. N)_AsyncConnDialect)_ConnectionFairy_ConnectionRecord)Pool)exc)util)chop_traceback)queue) threadingc@seZdZdZdZejZd!ddZdd Z d d Z d d Z ddZ ddZ ddZddZddZddZddZddZddZd S)" QueuePoolzA :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, unless the SQLite dialect is in use. F >@cKsDtj||f||j||d|_d||_||_||_t|_ dS)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. )use_liforN) r__init__ _queue_class_pool _overflow _max_overflow_timeoutr Lock_overflow_lock)selfcreator pool_size max_overflowtimeoutrkwr!UC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\pool\impl.pyr(s ? zQueuePool.__init__c CsFz|j|dWn.tjk r@z |W5|XYnXdSNF)rput sqla_queueFull _dec_overflowcloserconnr!r!r"_do_return_connns  zQueuePool._do_return_connc Cs|jdk}z"|o|j|jk}|j||jWStjk rBYnX|r|j|jkr|s`|Stj d| | |jfdd| rz | WSt|W5QRXYqXn|SdS)NzSQueuePool limit of size %d overflow %d reached, connection timed out, timeout %0.2fZ3o7r)code)rrrgetrr%Empty_do_getr TimeoutErrorsizeoverflow _inc_overflow_create_connectionr Z safe_reraiser')rZ use_overflowwaitr!r!r"r0ws,   zQueuePool._do_getc Csh|jdkr|jd7_dS|j<|j|jkrL|jd7_W5QRdSW5QRdSW5QRXdS)Nr,rTFrrrrr!r!r"r4s  zQueuePool._inc_overflowc CsJ|jdkr|jd8_dS|j|jd8_W5QRdSQRXdS)Nr,rTr7r8r!r!r"r's  zQueuePool._dec_overflowcCsJ|jd|j|j|jj|j|j|jj|j |j |j |j |j |j|jd S)NPool recreating) rrpre_pingrrrecycleecho logging_namereset_on_return _dispatchdialect)loggerinfo __class___creatorrmaxsizer _pre_pingrr_recycler<_orig_logging_name_reset_on_returndispatch_dialectr8r!r!r"recreates zQueuePool.recreatecCsZz|jd}|Wqtjk r2Yq6YqXqd||_|jd| dS)NFrzPool disposed. %s) rr.r(r%r/r2rrArBstatusr)r!r!r"disposes   zQueuePool.disposecCs d||||fS)Nz_Pool size: %d Connections in pool: %d Current Overflow: %d Current Checked out connections: %d)r2 checkedinr3 checkedoutr8r!r!r"rMszQueuePool.statuscCs|jjSN)rrEr8r!r!r"r2szQueuePool.sizecCs|jSrQ)rr8r!r!r"rszQueuePool.timeoutcCs |jSrQ)rqsizer8r!r!r"rOszQueuePool.checkedincCs|jSrQ)rr8r!r!r"r3szQueuePool.overflowcCs|jj|j|jSrQ)rrErRrr8r!r!r"rPszQueuePool.checkedoutN)rrrF)__name__ __module__ __qualname____doc__ _is_asyncior%Queuerrr+r0r4r'rLrNrMr2rrOr3rPr!r!r!r"rs( F    rc@seZdZdZejZeZdS)AsyncAdaptedQueuePoolTN) rSrTrUrWr%ZAsyncAdaptedQueuerrrKr!r!r!r"rYsrYc@seZdZejZdS)FallbackAsyncAdaptedQueuePoolN)rSrTrUr%ZFallbackAsyncAdaptedQueuerr!r!r!r"rZsrZc@s8eZdZdZddZddZddZdd Zd d Zd S) NullPoolaQA 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. cCsdS)Nr[r!r8r!r!r"rMszNullPool.statuscCs |dSrQ)r(r)r!r!r"r+szNullPool._do_return_conncCs|SrQ)r5r8r!r!r"r0szNullPool._do_getc Cs6|jd|j|j|j|j|j|j|j|j |j dS)Nr9)r;r<r=r>r:r?r@) rArBrCrDrGr<rHrIrFrJrKr8r!r!r"rLs zNullPool.recreatecCsdSrQr!r8r!r!r"rNszNullPool.disposeN) rSrTrUrVrMr+r0rLrNr!r!r!r"r[s  r[c@s^eZdZdZdZdddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ dS)SingletonThreadPoolaA 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. 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`. FrcKs6tj||f|t|_t|_t|_||_dSrQ) rrr local_conn_fairyset _all_connsr2)rrrr r!r!r"r5s   zSingletonThreadPool.__init__c Cs:|jd|j|j|j|j|j|j|j|j |j |j d S)Nr9)rr;r<r:r=r>r?r@) rArBrCrDr2rGr<rFrHrIrJrKr8r!r!r"rL<s zSingletonThreadPool.recreatec Cs<|jD]&}z |Wqtk r*YqXq|jdS)zDispose of this pool.N)rar( Exceptionclearr)r!r!r"rNJs   zSingletonThreadPool.disposecCs(t|j|jkr$|j}|qdSrQ)lenrar2popr(rcr!r!r"_cleanupWs zSingletonThreadPool._cleanupcCsdt|t|jfS)Nz"SingletonThreadPool id:%d size: %d)idrdrar8r!r!r"rM\szSingletonThreadPool.statuscCsdSrQr!r)r!r!r"r+bsz#SingletonThreadPool._do_return_conncCslz|j}|r|WSWntk r,YnX|}t||j_t|j|jkr\| |j ||SrQ) r^currentAttributeErrorr5weakrefrefrdrar2rhaddrfr!r!r"r0es   zSingletonThreadPool._do_getcCsBz|j}Wntk r"YnX|dk r4|St||jSrQ)r_rjrkZ_checkout_existingrZ _checkoutrZrecr!r!r"connectsszSingletonThreadPool.connectcCs.z |j`Wntk rYnX||dSrQ)r_rjrkr+)rrecordr!r!r" _return_conns  z SingletonThreadPool._return_connN)r)rSrTrUrVrWrrLrNrhrMr+r0rprrr!r!r!r"r\s   r\c@sVeZdZdZejddZddZddZdd Z d d Z d d Z ddZ ddZ dS) StaticPoolaA 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. cCst|SrQrr8r!r!r" connectionszStaticPool.connectioncCsdS)Nrsr!r8r!r!r"rMszStaticPool.statuscCs,d|jkr(|jjdk r(|j|jd=dSNrt)__dict__rtr(r8r!r!r"rNs   zStaticPool.disposec Cs6|jd|j|j|j|j|j|j|j|j |j dS)Nr9)rr;r>r:r<r=r?r@) rArBrCrDrGrIrFr<rHrJrKr8r!r!r"rLs zStaticPool.recreatecsfdd|_dS)NcsjjSrQ)rt)Zcrecother_static_poolr!r"z+StaticPool._transfer_from..)Z_invoke_creator)rrxr!rwr"_transfer_froms zStaticPool._transfer_fromcCs tdSrQ)NotImplementedErrorr8r!r!r"r5szStaticPool._create_connectioncCsdSrQr!r)r!r!r"r+szStaticPool._do_return_conncCs |j}|r|jd=|j}|Sru)rtZ_is_hard_or_soft_invalidatedrvror!r!r"r0s zStaticPool._do_getN)rSrTrUrVr Zmemoized_propertyrtrMrNrLr{r5r+r0r!r!r!r"rss   rsc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) 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. cOs6d|_d|_|dd|_d|_tj|f||dS)NFZstore_tracebackT)r^ _checked_outre_store_traceback_checkout_tracebackrr)rargsr r!r!r"rs zAssertionPool.__init__cCsdS)Nr}r!r8r!r!r"rMszAssertionPool.statuscCs&|jstdd|_||jks"tdS)Nzconnection is not checked outF)r~AssertionErrorr^r)r!r!r"r+szAssertionPool._do_return_conncCsd|_|jr|jdSr#)r~r^r(r8r!r!r"rNszAssertionPool.disposec Cs6|jd|j|j|j|j|j|j|j|j |j dS)Nr9)r<r:r;r>r=r?r@) rArBrCrDr<rFrGrIrHrJrKr8r!r!r"rLs zAssertionPool.recreatecCs^|jr2|jr"ddt|j}nd}td||jsB||_d|_|jrXt |_|jS)Nz at: %sz!connection is already checked outT) r~rjoinr rr^r5r traceback format_stack)rsuffixr!r!r"r0s   zAssertionPool._do_getN) rSrTrUrVrrMr+rNrLr0r!r!r!r"r}s  r})rVrrlbaserrrrrr r r r r%r rrYrZr[r\rsr}r!r!r!r" s&         H(t=