o
    hP                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 eeZG dd deZG d	d
 d
ejeddZG dd dZG dd dZG dd dZddddddddddeje
jdddZdS )    N   )compat
connection)
exceptions)protocolc                       s.   e Zd Zdd fdd
Zedd Z  ZS )PoolConnectionProxyMetaF)wrapc          	         s   |r?t tjD ]-}|ds||v rqttj|}t|sqt|}| ||}t	
||}|||< qd|vr?tjj|d< t | |||S )N___doc__)dirr   
Connection
startswithgetattrinspect
isfunctioniscoroutinefunction_wrap_connection_method	functoolsupdate_wrapperr   super__new__)	mclsnamebasesdctr	   attrnamemethiscoroutinewrapper	__class__ `/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/asyncpg/pool.pyr      s   


zPoolConnectionProxyMeta.__new__c                    s    fdd}|rt | |S )Nc                    s@   | j d u rtd t| j j }|| j g|R i |S )NzJcannot call Connection.{}(): connection has been released back to the pool)_conr   InterfaceErrorformatr   r!   )selfargskwargsr   	meth_namer"   r#   call_con_method0   s   
zHPoolConnectionProxyMeta._wrap_connection_method.<locals>.call_con_method)r   markcoroutinefunction)r+   r   r,   r"   r*   r#   r   .   s   
z/PoolConnectionProxyMeta._wrap_connection_method)__name__
__module____qualname__r   staticmethodr   __classcell__r"   r"   r    r#   r      s    r   c                   @   sD   e Zd ZdZdddejfddZdd Zd	ejfd
dZdd Z	dS )PoolConnectionProxy)r$   _holderholderPoolConnectionHolderconc                 C   s   || _ || _||  d S N)r$   r4   
_set_proxy)r'   r5   r7   r"   r"   r#   __init__G   s   zPoolConnectionProxy.__init__c                 C   s   t | j|S r8   )r   r$   )r'   attrr"   r"   r#   __getattr__M   s   zPoolConnectionProxy.__getattr__returnc                 C   s*   | j d u rd S | j d }| _ |d  |S r8   )r$   r9   )r'   r7   r"   r"   r#   _detachQ   s
   

zPoolConnectionProxy._detachc                 C   s:   | j d u rdj| jjt| dS dj| jj| j t| dS )Nz <{classname} [released] {id:#x}>)	classnameidz<{classname} {con!r} {id:#x}>)r?   r7   r@   )r$   r&   r!   r.   r@   r'   r"   r"   r#   __repr__Y   s   
zPoolConnectionProxy.__repr__N)
r.   r/   r0   	__slots__r   r   r:   r<   r>   rB   r"   r"   r"   r#   r3   A   s    
r3   T)	metaclassr	   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
efddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) r6   )r$   _pool_loop_proxy_max_queries_setup_max_inactive_time_in_use_inactive_callback_timeout_generationc                C   s@   || _ d | _d | _|| _|| _|| _d | _d | _d | _d | _	d S r8   )
rE   r$   rG   rH   rJ   rI   rL   rK   rM   rN   )r'   poolmax_queriessetupmax_inactive_timer"   r"   r#   r:   j   s   
zPoolConnectionHolder.__init__c                 C   s   | j d uo
| j   S r8   )r$   	is_closedrA   r"   r"   r#   is_connectedx   s   z!PoolConnectionHolder.is_connectedc                 C   s   | j  S r8   rK   rA   r"   r"   r#   is_idle{   s   zPoolConnectionHolder.is_idlec                    sF   | j d urtd| j I d H | _ | jj| _|   |   d S )NzMPoolConnectionHolder.connect() called while another connection already exists)r$   r   InternalClientErrorrE   _get_new_connectionrN   _maybe_cancel_inactive_callback_setup_inactive_callbackrA   r"   r"   r#   connect~   s   

zPoolConnectionHolder.connectr=   c              
      s   | j d u s| j  rd | _ |  I d H  n| j| jjkr4| jj| j j| jd d | _ |  I d H  | 	  t
| | j  | _}| jd urnz
| |I d H  W n ttjfym } zz| j  I d H  W ||d }~ww | jj | _|S )Ntimeout)r$   rS   r[   rN   rE   rF   create_taskcloserM   rY   r3   rG   rI   	ExceptionasyncioCancelledErrorcreate_futurerK   )r'   proxyexr"   r"   r#   acquire   s.   
zPoolConnectionHolder.acquirec              
      s  | j d u rtd| j rd S d | _| jjj| jkr)| jj	|dI d H  d S | j
| jj
kr<| jj	|dI d H  d S zi|}| jj rbt }t| jj |I d H  |d urb|t | 8 }| jjd urt|4 I d H  | j I d H  | j| jI d H  W d   I d H  n1 I d H sw   Y  n
| jj|dI d H  W n ttjfy } zz| j  W ||d }~ww |   |   d S )NzAPoolConnectionHolder.release() called on a free connection holderr\   )rK   r   rW   r$   rS   rM   	_protocolqueries_countrH   r_   rN   rE   _is_cancellingtime	monotonicr   wait_for_wait_for_cancellation_resetr]   resetr`   ra   rb   	terminate_releaserZ   )r'   r]   budgetstartedre   r"   r"   r#   release   sP   



(zPoolConnectionHolder.releasec                    s    | j d u rd S | j I d H  d S r8   rU   rA   r"   r"   r#   wait_until_released   s   
z(PoolConnectionHolder.wait_until_releasedc                    s$   | j d ur| j  I d H  d S d S r8   )r$   r_   rA   r"   r"   r#   r_      s   
zPoolConnectionHolder.closec                 C   s   | j d ur| j   d S d S r8   )r$   rp   rA   r"   r"   r#   rp      s   
zPoolConnectionHolder.terminatec                 C   s8   | j d ur
td| jr| jj| j| j| _ d S d S )Nz/pool connection inactivity timer already exists)rL   r   rW   rJ   rE   rF   
call_later_deactivate_inactive_connectionrA   r"   r"   r#   rZ      s   

z-PoolConnectionHolder._setup_inactive_callbackc                 C   s"   | j d ur| j   d | _ d S d S r8   )rL   cancelrA   r"   r"   r#   rY     s   


z4PoolConnectionHolder._maybe_cancel_inactive_callbackc                 C   s8   | j d ur
td| jd ur| j  |   d S d S )Nz/attempting to deactivate an acquired connection)rK   r   rW   r$   rp   _release_on_closerA   r"   r"   r#   rw     s   


z4PoolConnectionHolder._deactivate_inactive_connectionc                 C   s   |    |   d | _d S r8   )rY   rq   r$   rA   r"   r"   r#   ry     s   
z&PoolConnectionHolder._release_on_closec                 C   sV   | j du rdS | j  s| j d d| _ | jdur"| j  d| _| jj|  dS )zRelease this connection holder.N)rK   done
set_resultrG   r>   rE   _queue
put_nowaitrA   r"   r"   r#   rq      s   



zPoolConnectionHolder._releaseN)r.   r/   r0   rC   r:   rT   rV   r[   r3   rf   rt   ru   r_   rp   rZ   rY   rw   ry   rq   r"   r"   r"   r#   r6   b   s     %?	r6   c                   @   s  e Zd ZdZdZdddddddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdSddZdd ZdddededefddZdddedefd d!Zddd"defd#d$Zd%dd&d'd(Zddd"d)d*Zddd"d+d,Zddddddddddddd-d.d/Zddddddddddd0
d1d2Zddddddddddddddddd3d4d5Zddddd6d7d8Zddd9d:Zd;d< Zddd=d>Zd?d@ ZdAdB Z dCdD Z!dEdF Z"dGdH Z#dIdJ Z$dKdL Z%dMdN Z&dOdP Z'dQdR Z(dS )TPoola  A connection pool.

    Connection pool can be used to manage a set of connections to the database.
    Connections are first acquired from the pool, then used, and then released
    back to the pool.  Once a connection is released, it's reset to close all
    open cursors and other resources *except* prepared statements.

    Pools are created by calling :func:`~asyncpg.pool.create_pool`.
    )r|   rF   _minsize_maxsize_init_connectrn   _connect_args_connect_kwargs_holders_initialized_initializing_closing_closed_connection_class_record_classrN   rI   rH   !_max_inactive_connection_lifetimeN)r[   rQ   initro   c                O   s:  t |dkrtjdtdd |	d u rt }	|	| _|dkr!td|dk r)td||kr1td|dkr9td	|dk rAtd
t|
t	j
sNtd|
t|tjs[td||| _|| _g | _d| _d| _d | _|
| _|| _d| _d| _d| _|d ur|nt	j| _|| _|| _|| _|| _|| _ || _!|| _"d S )Nr   zPassing multiple positional arguments to asyncpg.Pool constructor is deprecated and will be removed in asyncpg 0.17.0.  The non-deprecated form is asyncpg.Pool(<dsn>, **kwargs)   )
stacklevelr   z,max_size is expected to be greater than zeroz3min_size is expected to be greater or equal to zeroz!min_size is greater than max_sizez/max_queries is expected to be greater than zerozKmax_inactive_connection_lifetime is expected to be greater or equal to zerozMconnection_class is expected to be a subclass of asyncpg.Connection, got {!r}zErecord_class is expected to be a subclass of asyncpg.Record, got {!r}F)#lenwarningswarnDeprecationWarningra   get_event_looprF   
ValueError
issubclassr   r   	TypeErrorr&   r   Recordr   r   r   r   r   r|   r   r   r   r   rN   r[   r   r   r   rI   r   rn   rH   r   )r'   min_sizemax_sizerP    max_inactive_connection_lifetimer[   rQ   r   ro   loopconnection_classrecord_classconnect_argsconnect_kwargsr"   r"   r#   r:   G  sh   zPool.__init__c                    sb   | j r| S | jrtd| jrtdd| _z|  I d H  | W d| _d| _ S d| _d| _ w )Nz)pool is being initialized in another taskpool is closedTF)r   r   r   r%   r   _initializerA   r"   r"   r#   _async__init__  s"   
zPool._async__init__c                    s   t j| jd| _t| jD ]}t| | j| j| jd}| j	
| | j| q| jrh| j	d }| I d H  | jdkrjg }tt| j	d d D ]\}}|| jd krV n|
|  qIt j| I d H  d S d S d S )N)maxsize)rP   rR   rQ   r   )ra   	LifoQueuer   r|   ranger6   rH   r   rI   r   appendr}   r   r[   	enumeratereversedgather)r'   r
   chfirst_chconnect_tasksir"   r"   r#   r     s.   

zPool._initializec                 C   s   | j p| jS )z_Return ``True`` if the pool is closing or is closed.

        .. versionadded:: 0.28.0
        )r   r   rA   r"   r"   r#   
is_closing  s   zPool.is_closingc                 C      t dd | jD S )zaReturn the current number of connections in this pool.

        .. versionadded:: 0.25.0
        c                 s   s    | ]}|  V  qd S r8   )rT   .0hr"   r"   r#   	<genexpr>  s    z Pool.get_size.<locals>.<genexpr>sumr   rA   r"   r"   r#   get_size     zPool.get_sizec                 C      | j S )zaReturn the minimum number of connections in this pool.

        .. versionadded:: 0.25.0
        )r   rA   r"   r"   r#   get_min_size     zPool.get_min_sizec                 C   r   )ziReturn the maximum allowed number of connections in this pool.

        .. versionadded:: 0.25.0
        )r   rA   r"   r"   r#   get_max_size  r   zPool.get_max_sizec                 C   r   )zfReturn the current number of idle connections in this pool.

        .. versionadded:: 0.25.0
        c                 s   s     | ]}|  o| V  qd S r8   )rT   rV   r   r"   r"   r#   r     s    z%Pool.get_idle_size.<locals>.<genexpr>r   rA   r"   r"   r#   get_idle_size  r   zPool.get_idle_sizec                 K   s   |g| _ || _dS )a  Set the new connection arguments for this pool.

        The new connection arguments will be used for all subsequent
        new connection attempts.  Existing connections will remain until
        they expire. Use :meth:`Pool.expire_connections()
        <asyncpg.pool.Pool.expire_connections>` to expedite the connection
        expiry.

        :param str dsn:
            Connection arguments specified using as a single string in
            the following format:
            ``postgres://user:pass@host:port/database?option=value``.

        :param \*\*connect_kwargs:
            Keyword arguments for the :func:`~asyncpg.connection.connect`
            function.

        .. versionadded:: 0.16.0
        N)r   r   )r'   dsnr   r"   r"   r#   set_connect_args  s   
zPool.set_connect_argsc              
      s   | j | j| j| j| jd| jI d H }t|| jsI| j}|j d|j }t	|}|jdkr4|j}n	|j d|j }t
d| d| d| jd urvz| |I d H  W |S  ttjfyu } zz
| I d H  W ||d }~ww |S )N)r   r   r   .builtinsz9expected pool connect callback to return an instance of 'z', got '')r   r   rF   r   r   r   
isinstancer/   r.   typer   r%   r   r`   ra   rb   r_   )r'   r7   goodgood_nbadbad_nre   r"   r"   r#   rX     sD   

zPool._get_new_connectionr\   queryr]   r=   c             	      s`   |   4 I dH }|j|g|R d|iI dH W  d  I dH  S 1 I dH s)w   Y  dS )a  Execute an SQL command (or commands).

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.execute() <asyncpg.connection.Connection.execute>`.

        .. versionadded:: 0.10.0
        Nr]   )rf   execute)r'   r   r]   r(   r7   r"   r"   r#   r   ,  s   	0zPool.executecommandc             	      sX   |   4 I dH }|j|||dI dH W  d  I dH  S 1 I dH s%w   Y  dS )aC  Execute an SQL *command* for each sequence of arguments in *args*.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.executemany()
        <asyncpg.connection.Connection.executemany>`.

        .. versionadded:: 0.10.0
        Nr\   )rf   executemany)r'   r   r(   r]   r7   r"   r"   r#   r   8  s   
0zPool.executemanyr]   r   c             	      b   |   4 I dH }|j|g|R ||dI dH W  d  I dH  S 1 I dH s*w   Y  dS )a-  Run a query and return the results as a list of :class:`Record`.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetch() <asyncpg.connection.Connection.fetch>`.

        .. versionadded:: 0.10.0
        Nr   )rf   fetchr'   r   r]   r   r(   r7   r"   r"   r#   r   E  s   0z
Pool.fetchr   columnr]   c             	      r   )a+  Run a query and return a value in the first row.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchval()
        <asyncpg.connection.Connection.fetchval>`.

        .. versionadded:: 0.10.0
        Nr   )rf   fetchval)r'   r   r   r]   r(   r7   r"   r"   r#   r   \  s   
0zPool.fetchvalc             	      r   )a  Run a query and return the first row.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchrow() <asyncpg.connection.Connection.fetchrow>`.

        .. versionadded:: 0.10.0
        Nr   )rf   fetchrowr   r"   r"   r#   r   j  s   	0zPool.fetchrowc             	      sZ   |   4 I dH }|j||||dI dH W  d  I dH  S 1 I dH s&w   Y  dS )an  Run a query for each sequence of arguments in *args*
        and return the results as a list of :class:`Record`.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchmany()
        <asyncpg.connection.Connection.fetchmany>`.

        .. versionadded:: 0.30.0
        Nr   )rf   	fetchmany)r'   r   r(   r]   r   r7   r"   r"   r#   r   {  s   
0zPool.fetchmany)columnsschema_namer]   r&   oids	delimiternullheaderquoteescapeforce_quoteencodingc                   sn   |   4 I dH !}|j|||||||||	|
||||dI dH W  d  I dH  S 1 I dH s0w   Y  dS )a;  Copy table contents to a file or file-like object.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_from_table()
        <asyncpg.connection.Connection.copy_from_table>`.

        .. versionadded:: 0.24.0
        N)outputr   r   r]   r&   r   r   r   r   r   r   r   r   )rf   copy_from_table)r'   
table_namer   r   r   r]   r&   r   r   r   r   r   r   r   r   r7   r"   r"   r#   r     s&   
0zPool.copy_from_table)
r]   r&   r   r   r   r   r   r   r   r   c                   st   |   4 I dH $}|j|g|R ||||||||	|
||dI dH W  d  I dH  S 1 I dH s3w   Y  dS )aC  Copy the results of a query to a file or file-like object.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_from_query()
        <asyncpg.connection.Connection.copy_from_query>`.

        .. versionadded:: 0.24.0
        N)r   r]   r&   r   r   r   r   r   r   r   r   )rf   copy_from_query)r'   r   r   r]   r&   r   r   r   r   r   r   r   r   r(   r7   r"   r"   r#   r     s(   0zPool.copy_from_query)r   r   r]   r&   r   freezer   r   r   r   r   r   force_not_null
force_nullr   wherec             	      s   |   4 I dH H}|j|fi d|d|d|d|d|d|d|d	|	d
|
d|d|d|d|d|d|d|d|I dH W  d  I dH  S 1 I dH sWw   Y  dS )a&  Copy data to the specified table.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_to_table()
        <asyncpg.connection.Connection.copy_to_table>`.

        .. versionadded:: 0.24.0
        Nsourcer   r   r]   r&   r   r   r   r   r   r   r   r   r   r   r   r   )rf   copy_to_table)r'   r   r   r   r   r]   r&   r   r   r   r   r   r   r   r   r   r   r   r   r7   r"   r"   r#   r     sP   	
0zPool.copy_to_table)r   r   r]   r   c             	      s^   |   4 I dH }|j||||||dI dH W  d  I dH  S 1 I dH s(w   Y  dS )aU  Copy a list of records to the specified table using binary COPY.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_records_to_table()
        <asyncpg.connection.Connection.copy_records_to_table>`.

        .. versionadded:: 0.24.0
        N)recordsr   r   r]   r   )rf   copy_records_to_table)r'   r   r   r   r   r]   r   r7   r"   r"   r#   r     s   
0zPool.copy_records_to_tablec                C   s
   t | |S )aT  Acquire a database connection from the pool.

        :param float timeout: A timeout for acquiring a Connection.
        :return: An instance of :class:`~asyncpg.connection.Connection`.

        Can be used in an ``await`` expression or with an ``async with`` block.

        .. code-block:: python

            async with pool.acquire() as con:
                await con.execute(...)

        Or:

        .. code-block:: python

            con = await pool.acquire()
            try:
                await con.execute(...)
            finally:
                await pool.release(con)
        )PoolAcquireContextr'   r]   r"   r"   r#   rf   4  s   
zPool.acquirec                    sR    fdd} j rtd   d u r| I d H S tj| dI d H S )Nc               	      sR    j  I d H } z	|  I d H }W n ttjfy#    j |   w | _|S r8   )r|   getrf   r`   ra   rb   r}   rM   )r   rd   r   r"   r#   _acquire_implN  s   z$Pool._acquire.<locals>._acquire_implzpool is closingr\   )r   r   r%   _check_initr   rl   )r'   r]   r   r"   r   r#   _acquireM  s   
zPool._acquirec                   sv   t |tus|jj| urtdj|d|jdu rdS |   |j	  |j}|du r0|j
}t||I dH S )a  Release a database connection back to the pool.

        :param Connection connection:
            A :class:`~asyncpg.connection.Connection` object to release.
        :param float timeout:
            A timeout for releasing the connection.  If not specified, defaults
            to the timeout provided in the corresponding call to the
            :meth:`Pool.acquire() <asyncpg.pool.Pool.acquire>` method.

        .. versionchanged:: 0.14.0
            Added the *timeout* parameter.
        zWPool.release() received invalid connection: {connection!r} is not a member of this poolr   N)r   r3   r4   rE   r   r%   r&   r$   r   _on_releaserM   ra   shieldrt   )r'   r   r]   r   r"   r"   r#   rt   e  s"   

zPool.releasec              	      s   | j rdS |   d| _d}zKz*| jd| j}dd | jD }tj| I dH  dd | jD }tj| I dH  W n t	tj
fyI   |    w W |durS|  d| _ d| _dS |durc|  d| _ d| _w )a  Attempt to gracefully close all connections in the pool.

        Wait until all pool connections are released, close them and
        shut down the pool.  If any error (including cancellation) occurs
        in ``close()`` the pool will terminate by calling
        :meth:`Pool.terminate() <pool.Pool.terminate>`.

        It is advisable to use :func:`python:asyncio.wait_for` to set
        a timeout.

        .. versionchanged:: 0.16.0
            ``close()`` now waits until all pool connections are released
            before closing them and the pool.  Errors raised in ``close()``
            will cause immediate pool termination.
        NT<   c                 S      g | ]}|  qS r"   )ru   r   r   r"   r"   r#   
<listcomp>      zPool.close.<locals>.<listcomp>c                 S   r   r"   r_   r   r"   r"   r#   r     r   F)r   r   r   rF   rv   _warn_on_long_closer   ra   r   r`   rb   rp   rx   )r'   warning_callbackrelease_corosclose_corosr"   r"   r#   r_     s>   
z
Pool.closec                 C   s   t d d S )NzPool.close() is taking over 60 seconds to complete. Check if you have any unreleased connections left. Use asyncio.wait_for() to set a timeout for Pool.close().)loggerwarningrA   r"   r"   r#   r     s   zPool._warn_on_long_closec                 C   s0   | j rdS |   | jD ]}|  qd| _ dS )z&Terminate all connections in the pool.NT)r   r   r   rp   r'   r   r"   r"   r#   rp     s   


zPool.terminatec                    s   |  j d7  _ dS )zExpire all currently open connections.

        Cause all currently open connections to get replaced on the
        next :meth:`~asyncpg.pool.Pool.acquire()` call.

        .. versionadded:: 0.16.0
        r   N)rN   rA   r"   r"   r#   expire_connections  s   zPool.expire_connectionsc                 C   s4   | j s| jrtdtd| jrtdd S )Nzipool is being initialized, but not yet ready: likely there is a race between creating a pool and using itzpool is not initializedr   )r   r   r   r%   r   rA   r"   r"   r#   r     s   

zPool._check_initc                 C   $   | j D ]}|jd ur|j  qd S r8   )r   r$   _drop_local_statement_cacher  r"   r"   r#   _drop_statement_cache  
   


zPool._drop_statement_cachec                 C   r  r8   )r   r$   _drop_local_type_cacher  r"   r"   r#   _drop_type_cache  r  zPool._drop_type_cachec                 C   s   |    S r8   )r   	__await__rA   r"   r"   r#   r	    s   zPool.__await__c                    s   |   I d H  | S r8   )r   rA   r"   r"   r#   
__aenter__  s   zPool.__aenter__c                    s   |   I d H  d S r8   r   )r'   excr"   r"   r#   	__aexit__  s   zPool.__aexit__r8   ))r.   r/   r0   r   rC   r:   r   r   r   r   r   r   r   r   rX   strfloatr   r   listr   r   r   r   r   r   r   r   rf   r   rt   r_   r   rp   r  r   r  r  r	  r
  r  r"   r"   r"   r#   r~   4  s    
Q!
(
2/:&-	
r~   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   )r]   r   rz   rO   c                 C   s   || _ || _d | _d| _d S )NF)rO   r]   r   rz   )r'   rO   r]   r"   r"   r#   r:     s   
zPoolAcquireContext.__init__c                    s8   | j d us	| jrtd| j| jI d H | _ | j S )Nz a connection is already acquired)r   rz   r   r%   rO   r   r]   rA   r"   r"   r#   r
    s
   
zPoolAcquireContext.__aenter__c                    s*   d| _ | j}d | _| j|I d H  d S NT)rz   r   rO   rt   )r'   r  r7   r"   r"   r#   r    s
   zPoolAcquireContext.__aexit__c                 C   s   d| _ | j| j S r  )rz   rO   r   r]   r	  rA   r"   r"   r#   r	  	  s   zPoolAcquireContext.__await__N)r.   r/   r0   rC   r:   r
  r  r	  r"   r"   r"   r#   r     s    r   
   iP  g     r@)r   r   rP   r   r[   rQ   r   ro   r   r   r   c                K   s(   t | f|
|||||	|||||d|S )a8  Create a connection pool.

    Can be used either with an ``async with`` block:

    .. code-block:: python

        async with asyncpg.create_pool(user='postgres',
                                       command_timeout=60) as pool:
            await pool.fetch('SELECT 1')

    Or to perform multiple operations on a single connection:

    .. code-block:: python

        async with asyncpg.create_pool(user='postgres',
                                       command_timeout=60) as pool:
            async with pool.acquire() as con:
                await con.execute('''
                   CREATE TABLE names (
                      id serial PRIMARY KEY,
                      name VARCHAR (255) NOT NULL)
                ''')
                await con.fetch('SELECT 1')

    Or directly with ``await`` (not recommended):

    .. code-block:: python

        pool = await asyncpg.create_pool(user='postgres', command_timeout=60)
        con = await pool.acquire()
        try:
            await con.fetch('SELECT 1')
        finally:
            await pool.release(con)

    .. warning::
        Prepared statements and cursors returned by
        :meth:`Connection.prepare() <asyncpg.connection.Connection.prepare>`
        and :meth:`Connection.cursor() <asyncpg.connection.Connection.cursor>`
        become invalid once the connection is released.  Likewise, all
        notification and log listeners are removed, and ``asyncpg`` will
        issue a warning if there are any listener callbacks registered on a
        connection that is being released to the pool.

    :param str dsn:
        Connection arguments specified using as a single string in
        the following format:
        ``postgres://user:pass@host:port/database?option=value``.

    :param \*\*connect_kwargs:
        Keyword arguments for the :func:`~asyncpg.connection.connect`
        function.

    :param Connection connection_class:
        The class to use for connections.  Must be a subclass of
        :class:`~asyncpg.connection.Connection`.

    :param type record_class:
        If specified, the class to use for records returned by queries on
        the connections in this pool.  Must be a subclass of
        :class:`~asyncpg.Record`.

    :param int min_size:
        Number of connection the pool will be initialized with.

    :param int max_size:
        Max number of connections in the pool.

    :param int max_queries:
        Number of queries after a connection is closed and replaced
        with a new connection.

    :param float max_inactive_connection_lifetime:
        Number of seconds after which inactive connections in the
        pool will be closed.  Pass ``0`` to disable this mechanism.

    :param coroutine connect:
        A coroutine that is called instead of
        :func:`~asyncpg.connection.connect` whenever the pool needs to make a
        new connection.  Must return an instance of type specified by
        *connection_class* or :class:`~asyncpg.connection.Connection` if
        *connection_class* was not specified.

    :param coroutine setup:
        A coroutine to prepare a connection right before it is returned
        from :meth:`Pool.acquire()`.  An example use
        case would be to automatically set up notifications listeners for
        all connections of a pool.

    :param coroutine init:
        A coroutine to initialize a connection when it is created.
        An example use case would be to setup type codecs with
        :meth:`Connection.set_builtin_type_codec() <\
        asyncpg.connection.Connection.set_builtin_type_codec>`
        or :meth:`Connection.set_type_codec() <\
        asyncpg.connection.Connection.set_type_codec>`.

    :param coroutine reset:
        A coroutine to reset a connection before it is returned to the pool by
        :meth:`Pool.release()`.  The function is supposed
        to reset any changes made to the database session so that the next
        acquirer gets the connection in a well-defined state.

        The default implementation calls :meth:`Connection.reset() <\
        asyncpg.connection.Connection.reset>`, which runs the following::

            SELECT pg_advisory_unlock_all();
            CLOSE ALL;
            UNLISTEN *;
            RESET ALL;

        The exact reset query is determined by detected server capabilities,
        and a custom *reset* implementation can obtain the default query
        by calling :meth:`Connection.get_reset_query() <\
        asyncpg.connection.Connection.get_reset_query>`.

    :param loop:
        An asyncio event loop instance.  If ``None``, the default
        event loop will be used.

    :return: An instance of :class:`~asyncpg.pool.Pool`.

    .. versionchanged:: 0.10.0
       An :exc:`~asyncpg.exceptions.InterfaceError` will be raised on any
       attempted operation on a released connection.

    .. versionchanged:: 0.13.0
       An :exc:`~asyncpg.exceptions.InterfaceError` will be raised on any
       attempted operation on a prepared statement or a cursor created
       on a connection that has been released to the pool.

    .. versionchanged:: 0.13.0
       An :exc:`~asyncpg.exceptions.InterfaceWarning` will be produced
       if there are any active listeners (added via
       :meth:`Connection.add_listener()
       <asyncpg.connection.Connection.add_listener>`
       or :meth:`Connection.add_log_listener()
       <asyncpg.connection.Connection.add_log_listener>`) present on the
       connection at the moment of its release to the pool.

    .. versionchanged:: 0.22.0
       Added the *record_class* parameter.

    .. versionchanged:: 0.30.0
       Added the *connect* and *reset* parameters.
    )r   r   r   r   rP   r   r[   rQ   r   ro   r   )r~   )r   r   r   rP   r   r[   rQ   r   ro   r   r   r   r   r"   r"   r#   create_pool  s$     r  r8   )ra   r   r   loggingrj   r    r   r   r   r   	getLoggerr.   r   r   r   _ConnectionProxyr3   r6   r~   r   r   r   r  r"   r"   r"   r#   <module>   sL   
)! S     D