B *®«]‰ã@s^dZddlZddlmZGdd„dejƒZGdd„deƒZGdd „d eƒZGd d „d eƒZ dS) z`Connection pooling for psycopg2 This module implements thread-safe (and not) connection pools. éN)Ú extensionsc@s eZdZdS)Ú PoolErrorN)Ú__name__Ú __module__Ú __qualname__©rrú@/opt/alt/python37/lib64/python3.7/site-packages/psycopg2/pool.pyrsrc@sFeZdZdZdd„Zddd„Zdd„Zdd d „Zdd d „Zdd„Z dS)ÚAbstractConnectionPoolzGeneric key-based pooling code.cOs^t|ƒ|_t|ƒ|_d|_||_||_g|_i|_i|_d|_ xt |jƒD] }|  ¡qJWdS)zèInitialize the connection pool. New 'minconn' connections are created immediately calling 'connfunc' with given parameters. The connection pool will support a maximum of about 'maxconn' connections. FrN) ÚintÚminconnÚmaxconnÚclosedÚ_argsÚ_kwargsÚ_poolÚ_usedÚ_rusedÚ_keysÚrangeÚ_connect)Úselfr r ÚargsÚkwargsÚirrrÚ__init__&s  zAbstractConnectionPool.__init__NcCsBtj|j|jŽ}|dk r2||j|<||jt|ƒ<n |j |¡|S)z;Create a new connection and assign it to 'key' if not None.N) Úpsycopg2ZconnectrrrrÚidrÚappend)rÚkeyÚconnrrrr<s   zAbstractConnectionPool._connectcCs|jd7_|jS)zReturn a new unique key.é)r)rrrrÚ_getkeyFszAbstractConnectionPool._getkeycCs„|jrtdƒ‚|dkr| ¡}||jkr2|j|S|jr^|j ¡|j|<}||jt|ƒ<|St|jƒ|j krvtdƒ‚|  |¡SdS)z9Get a free connection and assign it to 'key' if not None.zconnection pool is closedNzconnection pool exhausted) r rr!rrÚpoprrÚlenr r)rrrrrrÚ_getconnKs  zAbstractConnectionPool._getconnFcCsÊ|jrtdƒ‚|dkr6|j t|ƒ¡}|dkr6tdƒ‚t|jƒ|jkrš|sš|js¢|jj }|t j krl|  ¡q¢|t j krŒ| ¡|j |¡q¢|j |¡n|  ¡|jr²||jkrÆ|j|=|jt|ƒ=dS)zPut away a connection.zconnection pool is closedNz trying to put unkeyed connection)r rrÚgetrr#rr ÚinfoZtransaction_statusÚ_extZTRANSACTION_STATUS_UNKNOWNÚcloseZTRANSACTION_STATUS_IDLEZrollbackrr)rrrr(ZstatusrrrÚ_putconn^s&   zAbstractConnectionPool._putconnc CsX|jrtdƒ‚x>|jt|j ¡ƒD]&}y | ¡Wq$tk rHYq$Xq$Wd|_dS)z×Close all connections. Note that this can lead to some code fail badly when trying to use an already closed connection. If you call .closeall() make sure your code can deal with it. zconnection pool is closedTN)r rrÚlistrÚvaluesr(Ú Exception)rrrrrÚ _closealls  z AbstractConnectionPool._closeall)N)N)NF) rrrÚ__doc__rrr!r$r)r-rrrrr #s   #r c@s"eZdZdZejZejZej Z dS)ÚSimpleConnectionPoolz@A connection pool that can't be shared across different threads.N) rrrr.r r$Úgetconnr)Úputconnr-Úcloseallrrrrr/’sr/c@s4eZdZdZdd„Zd dd„Zd dd „Zd d „ZdS)ÚThreadedConnectionPoolz7A connection pool that works with the threading module.cOs,ddl}tj|||f|ž|Ž| ¡|_dS)zInitialize the threading lock.rN)Ú threadingr rZLockÚ_lock)rr r rrr4rrrrszThreadedConnectionPool.__init__NcCs&|j ¡z | |¡S|j ¡XdS)z9Get a free connection and assign it to 'key' if not None.N)r5Úacquirer$Úrelease)rrrrrr0¤s  zThreadedConnectionPool.getconnFcCs.|j ¡z| |||¡Wd|j ¡XdS)zPut away an unused connection.N)r5r6r)r7)rrrr(rrrr1¬s zThreadedConnectionPool.putconncCs(|j ¡z | ¡Wd|j ¡XdS)z6Close all connections (even the one currently in use.)N)r5r6r-r7)rrrrr2´s  zThreadedConnectionPool.closeall)N)NNF)rrrr.rr0r1r2rrrrr3šs   r3) r.rrr'ÚErrorrÚobjectr r/r3rrrrÚs  o