B 4]9@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@szeZdZdZd!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.  FcKsDtj||f|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_queueZQueue_pool _overflow _max_overflow_timeoutr ZLock_overflow_lock)selfcreator pool_size max_overflowtimeoutrkwrG/opt/alt/python37/lib64/python3.7/site-packages/sqlalchemy/pool/impl.pyr#s = zQueuePool.__init__c CsFy|j|dWn.tjk r@z |Wd|XYnXdS)NF)rZputrZFullclose _dec_overflow)rconnrrr_do_return_conngs  zQueuePool._do_return_connc Cs|jdk}y |o|j|jk}|j||jStjk r@YnX|r|j|jkr|s^|Stj d| | |jfdd| ry| St|WdQRXYqXn|SdS)NzPQueuePool limit of size %d overflow %d reached, connection timed out, timeout %dZ3o7r)code)rrrgetrrEmpty_do_getr TimeoutErrorsizeoverflow _inc_overflow_create_connectionrZ safe_reraiser!)rZ use_overflowwaitrrrr(ps(  zQueuePool._do_getc CsT|jdkr|jd7_dS|j(|j|jkrB|jd7_dSdSWdQRXdS)Nr$rTF)rrr)rrrrr,s  zQueuePool._inc_overflowc Cs@|jdkr|jd8_dS|j|jd8_dSQRXdS)Nr$rT)rrr)rrrrr!s  zQueuePool._dec_overflowc CsD|jd|j|j|jj|j|j|j|j |j |j |j |j |jd S)NzPool recreating) rrrrecycleecho logging_nameuse_threadlocalreset_on_return _dispatchdialect)loggerinfo __class___creatorrmaxsizerr_recycler0_orig_logging_name_use_threadlocal_reset_on_returndispatch_dialect)rrrrrecreates zQueuePool.recreatecCs\x6y|jd}|Wqtjk r2PYqXqWd||_|jd| dS)NFrzPool disposed. %s) rr&r rr'r*rr6r7status)rr"rrrdisposes   zQueuePool.disposecCs d||||fS)Nz_Pool size: %d Connections in pool: %d Current Overflow: %d Current Checked out connections: %d)r* checkedinr+ checkedout)rrrrrBs zQueuePool.statuscCs|jjS)N)rr:)rrrrr*szQueuePool.sizecCs|jS)N)r)rrrrrszQueuePool.timeoutcCs |jS)N)rqsize)rrrrrDszQueuePool.checkedincCs|jS)N)r)rrrrr+szQueuePool.overflowcCs|jj|j|jS)N)rr:rFr)rrrrrEszQueuePool.checkedoutN)r rrF)__name__ __module__ __qualname____doc__rr#r(r,r!rArCrBr*rrDr+rErrrrr s" >    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. cCsdS)NrKr)rrrrrBszNullPool.statuscCs |dS)N)r )rr"rrrr#szNullPool._do_return_conncCs|S)N)r-)rrrrr(szNullPool._do_getc Cs6|jd|j|j|j|j|j|j|j|j |j dS)NzPool recreating)r/r0r1r2r3r4r5) r6r7r8r9r;r0r<r=r>r?r@)rrrrrAs zNullPool.recreatecCsdS)Nr)rrrrrCszNullPool.disposeN) rGrHrIrJrBr#r(rArCrrrrrKs  rKc@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`. r cKs6tj||f|t|_t|_t|_||_dS)N) rrr Zlocal_conn_fairyset _all_connsr*)rrrrrrrr!s   zSingletonThreadPool.__init__c Cs:|jd|j|j|j|j|j|j|j|j |j |j d S)NzPool recreating)rr/r0r1r2r3r4r5) r6r7r8r9r*r;r0r<r=r>r?r@)rrrrrA(s zSingletonThreadPool.recreatec Cs@x0|jD]&}y |Wqtk r,YqXqW|jdS)zDispose of this pool.N)rPr Exceptionclear)rr"rrrrC6s    zSingletonThreadPool.disposecCs,x&t|j|jkr&|j}|qWdS)N)lenrPr*popr )rcrrr_cleanupCs zSingletonThreadPool._cleanupcCsdt|t|jfS)Nz"SingletonThreadPool id:%d size: %d)idrSrP)rrrrrBHszSingletonThreadPool.statuscCsdS)Nr)rr"rrrr#Nsz#SingletonThreadPool._do_return_conncCsjy|j}|r|SWntk r*YnX|}t||j_t|j|jkrZ| |j ||S)N) rMcurrentAttributeErrorr-weakrefrefrSrPr*rVadd)rrUrrrr(Qs  zSingletonThreadPool._do_getcCsBy|j}Wntk r"YnX|dk r4|St||jS)N)rNrXrYZ_checkout_existingrZ _checkout)rZrecrrrconnect_szSingletonThreadPool.connectcCs.y |j`Wntk rYnX||dS)N)rNrXrYr#)rrecordrrr _return_connks  z SingletonThreadPool._return_connN)r ) rGrHrIrJrrArCrVrBr#r(r]r_rrrrrLs   rLc@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. cCs|S)N)r9)rrrrrM~szStaticPool._conncCst|S)N)r)rrrr connectionszStaticPool.connectioncCsdS)Nr`r)rrrrrBszStaticPool.statuscCsd|jkr|jd|_dS)NrM)__dict__rMr )rrrrrCs  zStaticPool.disposec Cs6|jd|j|j|j|j|j|j|j|j |j dS)NzPool recreating)rr/r2r3r0r1r4r5) r6r7r8r9r;r=r>r0r<r?r@)rrrrrAs zStaticPool.recreatecCs|jS)N)rM)rrrrr-szStaticPool._create_connectioncCsdS)Nr)rr"rrrr#szStaticPool._do_return_conncCs|jS)N)ra)rrrrr(szStaticPool._do_getN)rGrHrIrJrZmemoized_propertyrMrarBrCrAr-r#r(rrrrr`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. cOs6d|_d|_|dd|_d|_tj|f||dS)NFZstore_tracebackT)rM _checked_outrT_store_traceback_checkout_tracebackrr)rargsrrrrrs zAssertionPool.__init__cCsdS)Nrcr)rrrrrBszAssertionPool.statuscCs|jstdd|_dS)Nzconnection is not checked outF)rdAssertionError)rr"rrrr#szAssertionPool._do_return_conncCsd|_|jr|jdS)NF)rdrMr )rrrrrCszAssertionPool.disposecCs*|jd|j|j|j|j|j|jdS)NzPool recreating)r0r1r4r5)r6r7r8r9r0r<r?r@)rrrrrAs zAssertionPool.recreatecCs^|jr2|jr"ddt|j}nd}td||jsB||_d|_|jrXt |_|jS)Nz at: %sz!connection is already checked outT) rdrfjoinr rhrMr-re traceback format_stack)rsuffixrrrr(s   zAssertionPool._do_getN) rGrHrIrJrrBr#rCrAr(rrrrrcs  rc)rJrkrZbaserrrrirrr r rr r rKrLr`rcrrrr s         A(q2