o znh9@sdZddlZddlZddlmZddlmZddlmZddlmZdd lm Z dd l m Z dd l m Z dd l m Z Gd ddeZGdddeZGdddeZGdddeZGdddeZdS)zPool implementation classes. N)_ConnectionFairy_ConnectionRecord)Pool)exc)util)chop_traceback)queue) threadingc@seZdZdZ    d!ddZdd Zd d Zd d ZddZddZ ddZ ddZ ddZ ddZ ddZddZddZd S)" QueuePoolzA :class:`.Pool` that imposes a limit on the number of open connections. :class:`.QueuePool` is the default pooling implementation used for all :class:`.Engine` objects, unless the SQLite dialect is in use.  FcKsHtj||fi|tj||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.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. :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.recycle`, :paramref:`.Pool.echo`, :paramref:`.Pool.reset_on_return` and others are passed to the :class:`.Pool` constructor. )use_liforN) r__init__ sqla_queueQueue_pool _overflow _max_overflow_timeoutr Lock_overflow_lock)selfcreator pool_size max_overflowtimeoutrkwr!?/usr/local/lib/python3.10/dist-packages/sqlalchemy/pool/impl.pyr#s = zQueuePool.__init__c CsJz |j|dWdStjy$z |W|YdS|wwNF)rputrFullclose _dec_overflowrconnr!r!r"_do_return_conngs zQueuePool._do_return_connc Cs|jdk}z|o |j|jk}|j||jWStjy Ynw|r@|j|jkr@|s/|Stj d| | |jfdd| rjz| WSt|WdYdS1sbwYYdS|S)NzPQueuePool limit of size %d overflow %d reached, connection timed out, timeout %d3o7r)code)rrrgetrrEmpty_do_getr TimeoutErrorsizeoverflow _inc_overflow_create_connectionr safe_reraiser')r use_overflowwaitr!r!r"r0ps0    &zQueuePool._do_getcCsv|jdkr|jd7_dS|j |j|jkr(|jd7_ WddS WddS1s4wYdS)Nr+rTFrrrrr!r!r"r4s  $zQueuePool._inc_overflowcCsX|jdkr|jd8_dS|j|jd8_ WddS1s%wYdS)Nr+rTr9r:r!r!r"r's $zQueuePool._dec_overflowc CsD|jd|j|j|jj|j|j|j|j |j |j |j |j |jd S)NPool recreating) rrrrecycleecho logging_nameuse_threadlocalreset_on_return _dispatchdialect)loggerinfo __class___creatorrmaxsizerr_recycler=_orig_logging_name_use_threadlocal_reset_on_returndispatch_dialectr:r!r!r"recreates zQueuePool.recreatecCsV z |jd}|Wn tjyYnwqd||_|jd| dS)NTFrzPool disposed. %s) rr.r&rr/r2rrCrDstatusr(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 checkedoutr:r!r!r"rOszQueuePool.statuscCs|jjSN)rrGr:r!r!r"r2zQueuePool.sizecC|jSrS)rr:r!r!r"rzQueuePool.timeoutcCs |jSrS)rqsizer:r!r!r"rQs zQueuePool.checkedincCrUrS)rr:r!r!r"r3rVzQueuePool.overflowcCs|jj|j|jSrS)rrGrWrr:r!r!r"rRszQueuePool.checkedoutN)rrrF)__name__ __module__ __qualname____doc__rr*r0r4r'rNrPrOr2rrQr3rRr!r!r!r"r s&  D     r c@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. cCdS)Nr\r!r:r!r!r"rOzNullPool.statuscCs |dSrS)r&r(r!r!r"r*s zNullPool._do_return_conncC|SrS)r5r:r!r!r"r0rTzNullPool._do_getc C6|jd|j|j|j|j|j|j|j|j |j dS)Nr;)r<r=r>r?r@rArB) rCrDrErFrHr=rIrJrKrLrMr:r!r!r"rNs zNullPool.recreatecCdSrSr!r:r!r!r"rPr^zNullPool.disposeN) rXrYrZr[rOr*r0rNrPr!r!r!r"r\s  r\c@sZeZdZdZdddZddZddZd d Zd d Zd dZ 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`, 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`. rcKs:tj||fi|t|_t|_t|_||_dSrS) rrr local_conn_fairyset _all_connsr2)rrrr r!r!r"r!s    zSingletonThreadPool.__init__c Cs:|jd|j|j|j|j|j|j|j|j |j |j d S)Nr;)rr<r=r>r?r@rArB) rCrDrErFr2rHr=rIrJrKrLrMr:r!r!r"rN(s zSingletonThreadPool.recreatec Cs8|jD]}z|WqtyYqw|jdS)zDispose of this pool.N)rgr& Exceptionclearr(r!r!r"rP6s   zSingletonThreadPool.disposecCs:t|j|jkr|j}|t|j|jksdSdSrS)lenrgr2popr&rcr!r!r"_cleanupCs zSingletonThreadPool._cleanupcCsdt|t|jfS)Nz"SingletonThreadPool id:%d size: %d)idrjrgr:r!r!r"rOHszSingletonThreadPool.statuscCrarSr!r(r!r!r"r*Nr^z#SingletonThreadPool._do_return_conncCsjz |j}|r |WSWn tyYnw|}t||j_t|j|jkr-| |j ||SrS) rdcurrentAttributeErrorr5weakrefrefrjrgr2rnaddrlr!r!r"r0Qs   zSingletonThreadPool._do_getcCs@z|j}Wn tyYn w|dur|St||jSrS)rerprq_checkout_existingr _checkout)rrecr!r!r"connect_s zSingletonThreadPool.connectcCs,z|j`Wn tyYnw||dSrS)rerprqr*)rrecordr!r!r" _return_connks   z SingletonThreadPool._return_connN)r) rXrYrZr[rrNrPrnrOr*r0rxrzr!r!r!r"rbs   rbc@s\eZdZdZejddZejddZddZdd Z d d Z d d Z ddZ ddZ 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 not currently supported by this Pool implementation but may be implemented in a future release. cCr_rS)rFr:r!r!r"rd~zStaticPool._conncCst|SrSrr:r!r!r" connectionr|zStaticPool.connectioncCr])Nr{r!r:r!r!r"rOr^zStaticPool.statuscCs"d|jvr|jd|_dSdS)Nrd)__dict__rdr&r:r!r!r"rPs   zStaticPool.disposec Cr`)Nr;)rr<r?r@r=r>rArB) rCrDrErFrHrJrKr=rIrLrMr:r!r!r"rNs zStaticPool.recreatecCrUrS)rdr:r!r!r"r5rVzStaticPool._create_connectioncCrarSr!r(r!r!r"r*r^zStaticPool._do_return_conncCrUrS)r}r:r!r!r"r0rVzStaticPool._do_getN)rXrYrZr[r memoized_propertyrdr}rOrPrNr5r*r0r!r!r!r"r{ss    r{c@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) AssertionPoolaA :class:`.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. cOs<d|_d|_|dd|_d|_tj|g|Ri|dS)NFstore_tracebackT)rd _checked_outrk_store_traceback_checkout_tracebackrr)rargsr r!r!r"rs zAssertionPool.__init__cCr])Nrr!r:r!r!r"rOr^zAssertionPool.statuscCs&|jstdd|_||jusJdS)Nzconnection is not checked outF)rAssertionErrorrdr(r!r!r"r*szAssertionPool._do_return_conncCsd|_|jr |jdSdSr#)rrdr&r:r!r!r"rPszAssertionPool.disposecCs*|jd|j|j|j|j|j|jdS)Nr;)r=r>rArB)rCrDrErFr=rIrLrMr:r!r!r"rNs zAssertionPool.recreatecCs^|jr|jrddt|j}nd}td||js!||_d|_|jr,t |_|jS)Nz at: %sz!connection is already checked outT) rrjoinr rrdr5r traceback format_stack)rsuffixr!r!r"r0s   zAssertionPool._do_getN) rXrYrZr[rrOr*rPrNr0r!r!r!r"rs  r)r[rrrbaserrrrrr r r rr r r\rbr{rr!r!r!r"s"        A(q2