U kfJ@sdZddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z ddlZd d lmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZddlmZddlmZddlmZddlmZddlmZej r@ddl m!Z!GdddeZ"Gddde"Z#Gd d!d!e#Z$Gd"d#d#eZ%Gd$d%d%eZ&Gd&d'd'eZ'Gd(d)d)eZ(dS)*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)DBAPIConnectionc@seZdZUdZdZejZded<ded<d1d d d d d ddddZ dddddZ ddddZ d dddZ ddddZ dddd Zddd!d"Zd#dd$d%Zd dd&d'Zd dd(d)Zd dd*d+Zd dd,d-Zd dd.d/Zd0S)2 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]_pool >@)Union[_CreatorFnType, _CreatorWRecFnType]intfloatboolr)creator pool_size max_overflowtimeoutuse_lifokwcKsPtj||f||j||d|_d||_|dkr6dn||_||_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. )r+rN) r__init__rr _overflow _max_overflow_timeout threadingLock_overflow_lock)selfr'r(r)r*r+r,r6D/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/pool/impl.pyr.Is @ zQueuePool.__init__rNonerecordreturnc CsFz|j|dWn.tjk r@z |W5|XYnXdSNF)rput sqla_queueFull _dec_overflowcloser5r:r6r6r7_do_return_conns  zQueuePool._do_return_connr;c Cs|jdk}|o|j|jk}z|j||jWStjk rBYnX|r|j|jkr|s`|Stj d| | |jfdd| rz | WSt|W5QRXYqXn|SdS)Nr-zSQueuePool limit of size %d overflow %d reached, connection timed out, timeout %0.2fZ3o7r)code)r0r/rgetr1r>Empty_do_getr TimeoutErrorsizeoverflow _inc_overflow_create_connectionrZ safe_reraiser@)r5Z use_overflowwaitr6r6r7rHs.    zQueuePool._do_getc Csh|jdkr|jd7_dS|j<|j|jkrL|jd7_W5QRdSW5QRdSW5QRXdS)Nr-r TFr0r/r4r5r6r6r7rLs  zQueuePool._inc_overflowz Literal[True]c CsJ|jdkr|jd8_dS|j|jd8_W5QRdSQRXdS)Nr-r TrOrPr6r6r7r@s  zQueuePool._dec_overflowcCsJ|jd|j|j|jj|j|j|jj|j |j |j |j |j |j|jd S)NPool recreating) r(r)pre_pingr+r*recycleecho logging_namereset_on_return _dispatchdialect)loggerinfo __class___creatorrmaxsizer0 _pre_pingr+r1_recyclerT_orig_logging_name_reset_on_returndispatch_dialectrPr6r6r7recreates zQueuePool.recreatecCsZz|jd}|Wqtjk r2Yq6YqXqd||_|jd| dS)NFrzPool disposed. %s) rrFrAr>rGrJr/rYrZstatusr5connr6r6r7disposes   zQueuePool.disposestrcCs d||||fS)Nz_Pool size: %d Connections in pool: %d Current Overflow: %d Current Checked out connections: %d)rJ checkedinrK checkedoutrPr6r6r7reszQueuePool.statuscCs|jjSN)rr]rPr6r6r7rJszQueuePool.sizecCs|jSrl)r1rPr6r6r7r*szQueuePool.timeoutcCs |jSrl)rqsizerPr6r6r7rjszQueuePool.checkedincCs|jjr|jSdS)Nr)rr]r/rPr6r6r7rKszQueuePool.overflowcCs|jj|j|jSrl)rr]rmr/rPr6r6r7rkszQueuePool.checkedoutN)r r!r"F)__name__ __module__ __qualname____doc__ _is_asyncior>Queuer__annotations__r.rCrHrLr@rdrhrerJr*rjrKrkr6r6r6r7r-s,  G     rc@s*eZdZUdZdZejZded<e Z dS)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`. TrrN) rnrorprqrrr>ZAsyncAdaptedQueuerrtr rcr6r6r6r7rus   ruc@seZdZejZdS)FallbackAsyncAdaptedQueuePoolN)rnrorpr>ZFallbackAsyncAdaptedQueuerr6r6r6r7rvsrvc@sXeZdZdZddddZdddd d Zddd d Zddd dZddddZdS)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`. rirDcCsdS)Nrwr6rPr6r6r7re-szNullPool.statusrr8r9cCs |dSrl)rArBr6r6r7rC0szNullPool._do_return_conncCs|Srl)rMrPr6r6r7rH3szNullPool._do_getc Cs6|jd|j|j|j|j|j|j|j|j |j dS)NrQ)rSrTrUrVrRrWrX) rYrZr[r\r_rTr`rar^rbrcrPr6r6r7rd6s zNullPool.recreatecCsdSrlr6rPr6r6r7rhDszNullPool.disposeN) rnrorprqrerCrHrdrhr6r6r6r7rws rwc@seZdZdZdZddddddd Zdd d d Zd d ddZd d ddZdd ddZ dd dddZ dd ddZ dd ddZ dS) 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,cKs6tj||f|t|_t|_t|_||_dSrl) rr.r2local_conn_fairyset _all_connsrJ)r5r'r(r,r6r6r7r.ks   zSingletonThreadPool.__init__rDc Cs:|jd|j|j|j|j|j|j|j|j |j |j d S)NrQ)r(rSrTrRrUrVrWrX) rYrZr[r\rJr_rTr^r`rarbrcrPr6r6r7rdws zSingletonThreadPool.recreater8c Cs<|jD]&}z |Wqtk r*YqXq|jdS)zDispose of this pool.N)r}rA Exceptionclearrfr6r6r7rhs   zSingletonThreadPool.disposecCs(t|j|jkr$|j}|qdSrl)lenr}rJpoprAr5cr6r6r7_cleanups zSingletonThreadPool._cleanupricCsdt|t|jfS)Nz"SingletonThreadPool id:%d size: %d)idrr}rPr6r6r7reszSingletonThreadPool.statusrr9cCs$z |j`Wntk rYnXdSrl)r{currentAttributeErrorrBr6r6r7rCs z#SingletonThreadPool._do_return_conncCsz.trtt|j}n |j}|r,|WSWntk rBYnX|}t||j_t |j |j krr| |j ||Srl)r rrrzrrrMweakrefrefrr}rJraddrr6r6r7rHs   zSingletonThreadPool._do_getrcCsHztt|j}Wntk r(YnX|dk r:|St||jSrl)rr r{rrZ_checkout_existingZ _checkoutr5recr6r6r7connectszSingletonThreadPool.connectN)r ) rnrorprqrrr.rdrhrrerCrHrr6r6r6r7rxHs   rxc@seZdZdZejddddZddddZd dd d Zddd d Z dd dddZ ddddZ dd dddZ ddddZ dS) 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`. rrDcCst|SrlrrPr6r6r7 connectionszStaticPool.connectionricCsdS)Nrr6rPr6r6r7reszStaticPool.statusr8cCs,d|jkr(|jjdk r(|j|jd=dSNr)__dict__rdbapi_connectionrArPr6r6r7rhs   zStaticPool.disposec Cs6|jd|j|j|j|j|j|j|j|j |j dS)NrQ)r'rSrVrRrTrUrWrX) rYrZr[r\r_rar^rTr`rbrcrPr6r6r7rds zStaticPool.recreate)other_static_poolr;csdddfdd }||_dS)Nrr)rr;csjj}|dk st|Srl)rrAssertionError)rrgrr6r7r's z*StaticPool._transfer_from..creator)Z_invoke_creator)r5rr'r6rr7_transfer_fromszStaticPool._transfer_fromrcCs tdSrl)NotImplementedErrorrPr6r6r7rMszStaticPool._create_connectionr9cCsdSrlr6rBr6r6r7rCszStaticPool._do_return_conncCs |j}|r|jd=|j}|Sr)rZ_is_hard_or_soft_invalidatedrrr6r6r7rHs zStaticPool._do_getN)rnrorprqrZmemoized_propertyrrerhrdrrMrCrHr6r6r6r7rs   rc@szeZdZUdZded<ded<ddddd Zd d d d ZdddddZdd ddZdd ddZ dd ddZ 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. The :class:`.AssertionPool` class **is compatible** with asyncio and :func:`_asyncio.create_async_engine`. zOptional[ConnectionPoolEntry]rzzOptional[List[str]]_checkout_tracebackr)argsr,cOs6d|_d|_|dd|_d|_tj|f||dS)NFZstore_tracebackT)rz _checked_outr_store_tracebackrrr.)r5rr,r6r6r7r.s zAssertionPool.__init__rirDcCsdS)Nrr6rPr6r6r7reszAssertionPool.statusrr8r9cCs&|jstdd|_||jks"tdS)Nzconnection is not checked outF)rrrzrBr6r6r7rCszAssertionPool._do_return_conncCsd|_|jr|jdSr<)rrzrArPr6r6r7rh#szAssertionPool.disposec Cs6|jd|j|j|j|j|j|j|j|j |j dS)NrQ)rTrRrSrVrUrWrX) rYrZr[r\rTr^r_rar`rbrcrPr6r6r7rd(s zAssertionPool.recreatecCs^|jr2|jr"ddt|j}nd}td||jsB||_d|_|jrXt |_|jS)Nz at: %sz!connection is already checked outT) rrjoinrrrzrMr traceback format_stack)r5suffixr6r6r7rH5s   zAssertionPool._do_getN) rnrorprqrtr.rerCrhrdrHr6r6r6r7rs   r))rq __future__rr2rtypingrrrrrrr r rbaser r rrrrrrrrrrrr>Z util.typingrZengine.interfacesrrrurvrwrxrrr6r6r6r7 sH                       Y*zA