U ¡Ï a¬ã@s\dZddlZddlmZGdd„dejƒZGdd„dƒZGdd „d eƒZGd d „d eƒZdS) z`Connection pooling for psycopg2 This module implements thread-safe (and not) connection pools. éN)Ú extensionsc@s eZdZdS)Ú PoolErrorN)Ú__name__Ú __module__Ú __qualname__©rrúNC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-8c51wohf\psycopg2\pool.pyr src@sFeZdZdZdd„Zddd„Zdd„Zdd d „Zdd d „Zdd„Z dS)ÚAbstractConnectionPoolzGeneric key-based pooling code.cOsZt|ƒ|_t|ƒ|_d|_||_||_g|_i|_i|_d|_ t |jƒD] }|  ¡qHdS)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) Úpsycopg2ÚconnectrrrrÚidrÚappend©rÚkeyÚconnrrrr=s   zAbstractConnectionPool._connectcCs|jd7_|jS)zReturn a new unique key.é)r©rrrrÚ_getkeyGszAbstractConnectionPool._getkeycCs„|jrtdƒ‚|dkr| ¡}||jkr2|j|S|jr^|j ¡|j|<}||jt|ƒ<|St|jƒ|j krvtdƒ‚|  |¡SdS)ú9Get a free connection and assign it to 'key' if not None.úconnection pool is closedNzconnection pool exhausted) r rr$rrÚpoprrÚlenr rrrrrÚ_getconnLs  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.r&Nz trying to put unkeyed connection)r rrÚgetrr(rr ÚinfoZtransaction_statusÚ_extZTRANSACTION_STATUS_UNKNOWNÚcloseZTRANSACTION_STATUS_IDLEÚrollbackrr)rr!r r-ÚstatusrrrÚ_putconn_s&   zAbstractConnectionPool._putconnc CsT|jrtdƒ‚|jt|j ¡ƒD]&}z | ¡Wq"tk rFYq"Xq"d|_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. r&TN)r rrÚlistrÚvaluesr-Ú Exception)rr!rrrÚ _closeall‚s z AbstractConnectionPool._closeall)N)N)NF) rrrÚ__doc__rrr$r)r0r4rrrrr $s   #r c@s"eZdZdZejZejZej Z dS)ÚSimpleConnectionPoolz@A connection pool that can't be shared across different threads.N) rrrr5r r)Úgetconnr0Úputconnr4Úcloseallrrrrr6“sr6c@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 rÚLockÚ_lock)rr r rrr;rrrržsÿÿÿzThreadedConnectionPool.__init__NcCs*|j ¡z| |¡W¢S|j ¡XdS)r%N)r=ÚacquireÚreleaser))rr rrrr7¥s zThreadedConnectionPool.getconnFcCs.|j ¡z| |||¡W5|j ¡XdS)zPut away an unused connection.N)r=r>r?r0)rr!r r-rrrr8­s zThreadedConnectionPool.putconncCs(|j ¡z | ¡W5|j ¡XdS)z6Close all connections (even the one currently in use.)N)r=r>r?r4r#rrrr9µs  zThreadedConnectionPool.closeall)N)NNF)rrrr5rr7r8r9rrrrr:›s   r:) r5rrr,ÚErrorrr r6r:rrrrÚs  o