L iIdZddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z ddlZd d lmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZddlmZddlmZddlmZddlmZddlmZejrddl m!Z!GddeZ"Gdde"Z#Gd d!e#Z$Gd"d#eZ%Gd$d%eZ&Gd&d'eZ'Gd(d)eZ(y)*zPool 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)DBAPIConnectionceZdZUdZdZej Zded<ded< d ddZ ddZ dd Z dd Z dd Z dd Zdd ZddZddZddZddZddZddZy) QueuePoolaOA :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]_poolc tj||fi||j|||_d|z |_|dk(rdn||_||_tj|_ y)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__r r! _overflow _max_overflow_timeout threadingLock_overflow_lock)selfcreator pool_size max_overflowtimeoutr#kws Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/pool/impl.pyr%zQueuePool.__init__Gs`@ dG*r*&&y8&D Y#,>R| 'nn.c |jj|dy#tj$r9 |j |j Yy#|j wxYwwxYwNF)r!put sqla_queueFullclose _dec_overflowr,records r2_do_return_connzQueuePool._do_return_connsS % JJNN65 ) % % ""$""$  %s A+AA+A''A+c|jdkD}|xr|j|jk\} |jj||jS#t j $rYnwxYw|rn|j|jk\rU|s|jStjd|j|j|jfzd|jrP |jS#tj5|j!ddd#1swYxYwxYw|jS)Nr$zSQueuePool limit of size %d overflow %d reached, connection timed out, timeout %0.2f3o7r)code)r'r&r!getr(r7Empty_do_getr TimeoutErrorsizeoverflow _inc_overflow_create_connectionr safe_reraiser:)r, use_overflowwaits r2rCzQueuePool._do_gets'))B. D$2D2D D ::>>$ 6 6     DNNd.@.@@||~%&&:yy{DMMOT]]CD      ..00 &&()&&())<<> !s5%AA*)A*.C>>D:D.% D:.D7 3D:c|jdk(r|xjdz c_y|j5|j|jkr|xjdz c_ dddy dddy#1swYyxYw)Nr$r TFr'r&r+r,s r2rGzQueuePool._inc_overflowsq    # NNa N   ~~ 2 22!#       s0A5+A55A>c|jdk(r|xjdzc_y|j5|xjdzc_ dddy#1swYyxYw)Nr$r TrMrNs r2r:zQueuePool._dec_overflowsP    # NNa N    NNa N   s AAc|jjd|j|j|jj |j |j|jj|j|j|j|j|j|j|j S)NPool recreating) r.r/pre_pingr#r0recycleecho logging_namereset_on_return _dispatchdialect)loggerinfo __class___creatorr!maxsizer' _pre_pingr#r(_recyclerT_orig_logging_name_reset_on_returndispatch_dialectrNs r2recreatezQueuePool.recreates *+~~ MMjj((++^^ZZ((MMMM00 11mmMM  r3c |jjd}|j-#tj$rYnwxYwd|j z |_|jjd|jy)NFrzPool disposed. %s) r!rAr9r7rBrEr&rYrZstatusr,conns r2disposezQueuePool.disposesr zz~~e, ##  TYY[ ,dkkm    !   r3c.|jjSN)r!r]rNs r2rEzQueuePool.sizeszz!!!r3c|jSrn)r(rNs r2r0zQueuePool.timeouts }}r3c6|jjSrn)r!qsizerNs r2rkzQueuePool.checkedinszz!!r3cJ|jjr |jSdS)Nr)r!r]r&rNs r2rFzQueuePool.overflows!%!3!3t~~::r3c~|jj|jjz |jzSrn)r!r]rqr&rNs r2rlzQueuePool.checkedouts-zz!!DJJ$4$4$66GGr3N) g>@F) r-)Union[_CreatorFnType, _CreatorWRecFnType]r.intr/rwr0floatr#boolr1rr<rreturnNoner{r)r{ry)r{z Literal[True])r{rr{r|r{str)r{rw)r{rx)__name__ __module__ __qualname____doc__ _is_asyncior7Queuer __annotations__r%r=rCrGr:rdrirfrEr0rkrFrlr3r2rr+s&K C 76  E/:E/E/ E/  E/  E/E/N%"@  " =  "";Hr3rcFeZdZUdZdZej Zded<e Z y)AsyncAdaptedQueuePoolaAn 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`. Trr N) rrrrrr7AsyncAdaptedQueuer rr rcrr3r2rrs. K$$C!"Hr3rc$eZdZejZy)FallbackAsyncAdaptedQueuePoolN)rrrr7FallbackAsyncAdaptedQueuer rr3r2rrs 77Lr3rc8eZdZdZddZd dZd dZd dZd dZy) NullPoolaA 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`. cy)NrrrNs r2rfzNullPool.status+sr3c$|jyrn)r9r;s r2r=zNullPool._do_return_conn.s  r3c"|jSrn)rHrNs r2rCzNullPool._do_get1s&&((r3c  |jjd|j|j|j|j |j |j|j|j|jS)NrQ)rSrTrUrVrRrWrX) rYrZr[r\r_rTr`rar^rbrcrNs r2rdzNullPool.recreate4sg *+~~ MMMM00 11^^mmMM  r3cyrnrrNs r2rizNullPool.disposeB r3Nrrzr})r{rr~) rrrrrfr=rCrdrirr3r2rrs  )   r3rcbeZdZdZdZ d d dZddZddZddZddZ ddZ dd Z dd Z y )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`. Fc tj||fi|tj|_tj|_t |_||_yrn) rr%r)local_conn_fairyset _all_connsrE)r,r-r.r1s r2r%zSingletonThreadPool.__init__isC dG*r*__& oo' 47E r3c  |jjd|j|j|j|j |j |j|j|j|j|j S)NrQ)r.rSrTrRrUrVrWrX) rYrZr[r\rEr_rTr^r`rarbrcrNs r2rdzSingletonThreadPool.recreateusn *+~~ MMiiMM^^00 11mmMM  r3c|jD]} |j|jjy#t$rY A  A ct|j|jk\rN|jj}|j t|j|jk\rMyyrn)lenrrEpopr9r,cs r2_cleanupzSingletonThreadPool._cleanupsI$//"dii/##%A GGI$//"dii/r3cHdt|t|jfzS)Nz"SingletonThreadPool id:%d size: %d)idrrrNs r2rfzSingletonThreadPool.statuss'3 tH  7   r3c< |j`y#t$rYywxYwrn)rcurrentAttributeErrorr;s r2r=z#SingletonThreadPool._do_return_conns#  #   s  c tr)tt|jj }n|jj }|r|S |j }tj||j_t|j|jk\r|j|jj||S#t $rYwxYwrn)r rrrrrrHweakrefrefrrrEraddrs r2rCzSingletonThreadPool._do_gets ,djj.@.@.BCJJ&&(  # # %$[[^  t 499 , MMO A   sA C CCc tt|jj}||j Stj ||jS#t $rY+wxYwrn)rrrr_checkout_existingr _checkoutr,recs r2connectzSingletonThreadPool.connectsb 0')<)<)>?C--//))$ <<    s(A A('A(N)rt)r-rvr.rwr1r)r{rr~rrzr})r{r) rrrrrr%rdrirrfr=rCrrr3r2rrFsX@K  :          " =r3rcneZdZdZej d dZd dZd dZddZ ddZ ddZ ddZ dd Z y ) 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`. ct|SrnrrNs r2 connectionzStaticPool.connections  &&r3cy)NrrrNs r2rfzStaticPool.statussr3cd|jvr?|jj(|jj|jd=yyyNr)__dict__rdbapi_connectionr9rNs r2rizStaticPool.disposesD DMM )00< OO ! ! # l+= *r3c  |jjd|j|j|j|j |j |j|j|j|jS)NrQ)r-rSrVrRrTrUrWrX) rYrZr[r\r_rar^rTr`rbrcrNs r2rdzStaticPool.recreatesg *+~~MMMM 11^^00mmMM  r3c dfd }||_y)Nc<jj}|J|Srn)rr)rrhother_static_pools r2r-z*StaticPool._transfer_from..creators%$//@@D# ##Kr3)rrr{r)_invoke_creator)r,rr-s ` r2_transfer_fromzStaticPool._transfer_froms  'r3ctrn)NotImplementedErrorrNs r2rHzStaticPool._create_connections !##r3cyrnrr;s r2r=zStaticPool._do_return_connrr3cp|j}|jr|jd=|j}|Sr)r_is_hard_or_soft_invalidatedrrs r2rCzStaticPool._do_gets1oo  + + - l+//C r3N)r{rrr~)r{r)rrr{r|r}rz)rrrrrmemoized_propertyrrfrirdrrHr=rCrr3r2rrsD  '',  '$ r3rcVeZdZUdZded<ded<d dZddZddZdd Zdd Z dd Z y ) 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`. zOptional[ConnectionPoolEntry]rzOptional[List[str]]_checkout_tracebackcd|_d|_|jdd|_d|_t j |g|i|y)NFstore_tracebackT)r _checked_outr_store_tracebackrrr%)r,argsr1s r2r%zAssertionPool.__init__sC ! "'8$ ?#'  d(T(R(r3cy)NrrrNs r2rfzAssertionPool.statussr3c`|js tdd|_||jusJy)Nzconnection is not checked outF)rAssertionErrorrr;s r2r=zAssertionPool._do_return_conns1   !@A A!###r3c`d|_|jr|jjyyr5)rrr9rNs r2rizAssertionPool.dispose!s&! :: JJ    r3c  |jjd|j|j|j|j |j |j|j|j|jS)NrQ)rTrRrSrVrUrWrX) rYrZr[r\rTr^r_rar`rbrcrNs r2rdzAssertionPool.recreate&sg *+~~ MM^^MM 1100mmMM  r3cT|jrD|jr(ddjt|jz}nd}t d|z|j s|j |_d|_|jrtj|_|j S)Nz at: %sz!connection is already checked outT) rrjoinrrrrHr traceback format_stack)r,suffixs r2rCzAssertionPool._do_get3s   ''#bgg"4#;#;<' !Dv!MN Nzz002DJ   '0'='='?D $zzr3N)rrr1rrrzr~)r{rr}) rrrrrr%rfr=rirdrCrr3r2rrs3  )(,,)$   r3r))r __future__rr)rtypingrrrrrr r r rbaser rrrrrrrrrrrrr7 util.typingrengine.interfacesrrrrrrrrrr3r2rs#"  #"# $%'!&! 3UHUHp#I#*8$98' t' Tw=$w=t>>BBDBr3