o a@s\dZddlZddlmZGdddejZGdddZGdd d eZGd d d eZdS) z`Connection pooling for psycopg2 This module implements thread-safe (and not) connection pools. N) extensionsc@s eZdZdS) PoolErrorN)__name__ __module__ __qualname__rr//usr/lib/python3/dist-packages/psycopg2/pool.pyr src@sFeZdZdZddZdddZddZdd d Zdd d ZddZ dS)AbstractConnectionPoolzGeneric key-based pooling code.cOsZt||_t||_d|_||_||_g|_i|_i|_d|_ t |jD]}| q$dS)zInitialize 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) intminconnmaxconnclosed_args_kwargs_pool_used_rused_keysrange_connect)selfr r argskwargsirrr__init__'s   zAbstractConnectionPool.__init__NcCsHtj|ji|j}|dur||j|<||jt|<|S|j||S)z;Create a new connection and assign it to 'key' if not None.N) psycopg2connectrrrridrappendrkeyconnrrrr=s  zAbstractConnectionPool._connectcCs|jd7_|jS)zReturn a new unique key.)rrrrr_getkeyGszAbstractConnectionPool._getkeycCs|jrtd|dur|}||jvr|j|S|jr/|j|j|<}||jt|<|St|j|j kr;td| |S)9Get a free connection and assign it to 'key' if not None.connection pool is closedNzconnection pool exhausted) r rr$rrpoprrlenr rrrrr_getconnLs   zAbstractConnectionPool._getconnFcCs|jrtd|dur|jt|}|durtdt|j|jkrM|sM|jsL|jj }|t j kr6| n|t j krF||j|n |j|n| |jrY||jvre|j|=|jt|=dSdS)zPut away a connection.r&Nz trying to put unkeyed connection)r rrgetrr(rr infotransaction_status_extTRANSACTION_STATUS_UNKNOWNcloseTRANSACTION_STATUS_IDLErollbackrr)rr!r r/statusrrr_putconn_s*    zAbstractConnectionPool._putconnc CsP|jrtd|jt|jD]}z|Wqty"Yqwd|_dS)zClose 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 rrlistrvaluesr/ Exception)rr!rrr _closealls   z AbstractConnectionPool._closeallN)NF) rrr__doc__rrr$r)r3r7rrrrr $s    #r c@s"eZdZdZejZejZej Z dS)SimpleConnectionPoolz@A connection pool that can't be shared across different threads.N) rrrr9r r)getconnr3putconnr7closeallrrrrr:s  r:c@s4eZdZdZddZd ddZd dd Zd d ZdS)ThreadedConnectionPoolz7A connection pool that works with the threading module.cOs2ddl}tj|||g|Ri|||_dS)zInitialize the threading lock.rN) threadingr rLock_lock)rr r rrr?rrrrszThreadedConnectionPool.__init__NcCs.|jz ||W|jS|jw)r%)rAacquirer)release)rr rrrr;s  zThreadedConnectionPool.getconnFcCs6|jz||||W|jdS|jw)zPut away an unused connection.N)rArBr3rC)rr!r r/rrrr<s zThreadedConnectionPool.putconncCs0|jz |W|jdS|jw)z6Close all connections (even the one currently in use.)N)rArBr7rCr#rrrr=s  zThreadedConnectionPool.closeallr8)NNF)rrrr9rr;r<r=rrrrr>s    r>) r9rrr-Errorrr r:r>rrrrs o