ok

Mini Shell

Direktori : /opt/alt/python38/lib64/python3.8/site-packages/playhouse/__pycache__/
Upload File :
Current File : //opt/alt/python38/lib64/python3.8/site-packages/playhouse/__pycache__/pool.cpython-38.pyc

U

S��W	 �@s.dZddlZddlZddlZddlmZddlmZddlmZe�d�Z	dd�Z
Gd	d
�d
e�ZGdd�dee�Z
Gd
d�de�ZGdd�dee�Zz"ddlmZGdd�dee�ZWnek
r�YnXGdd�de�ZGdd�dee�Zz"ddlmZGdd�dee�ZWnek
�r(YnXdS)ap	
Lightweight connection pooling for peewee.

In a multi-threaded application, up to `max_connections` will be opened. Each
thread (or, if using gevent, greenlet) will have it's own connection.

In a single-threaded application, only one connection will be created. It will
be continually recycled until either it exceeds the stale timeout or is closed
explicitly (using `.manual_close()`).

By default, all your application needs to do is ensure that connections are
closed when you are finished with them, and they will be returned to the pool.
For web applications, this typically means that at the beginning of a request,
you will open a connection, and when you return a response, you will close the
connection.

Simple Postgres pool example code:

    # Use the special postgresql extensions.
    from playhouse.pool import PooledPostgresqlExtDatabase

    db = PooledPostgresqlExtDatabase(
        'my_app',
        max_connections=32,
        stale_timeout=300,  # 5 minutes.
        user='postgres')

    class BaseModel(Model):
        class Meta:
            database = db

That's it!

In some situations you may want to manage your connections more explicitly.
Since peewee stores the active connection in a threadlocal, this typically
would mean that there could only ever be one connection open per thread. For
most applications this is desirable, but if you would like to manually manage
multiple connections you can create an *ExecutionContext*.

Execution contexts allow finer-grained control over managing multiple
connections to the database. When an execution context is initialized (either
as a context manager or as a decorated function), a separate connection will
be used for the duration of the wrapped block. You can also choose whether to
wrap the block in a transaction.

Execution context examples (using above `db` instance):

    with db.execution_context() as ctx:
        # A new connection will be opened or pulled from the pool of available
        # connections. Additionally, a transaction will be started.
        user = User.create(username='charlie')

    # When the block ends, the transaction will be committed and the connection
    # will be returned to the pool.

    @db.execution_context(with_transaction=False)
    def do_something(foo, bar):
        # When this function is called, a separate connection is made and will
        # be closed when the function returns.
�N)�
MySQLDatabase)�PostgresqlDatabase)�SqliteDatabasezpeewee.poolcCs"|dk	rt|ttf�st|�S|S�N)�
isinstance�int�float)�val�r
�/pool.py�make_intHsrcsfeZdZd�fdd�	Zd�fdd�	Z�fdd�Zd	d
�Zdd�Zd�fdd�	Zdd�Z	dd�Z
�ZS)�PooledDatabase�NcsFt|�|_t|�|_g|_i|_t�|_t|_t	t
|�j|f|�dSr)r�max_connections�
stale_timeout�_connections�_in_use�set�_closed�id�conn_key�superr
�__init__)�self�databaserr�kwargs��	__class__r
rrOs

zPooledDatabase.__init__cs<tt|�j|f|�|dk	r&t|�|_|dk	r8t|�|_dSr)rr
�initrrr)rrrrZconnect_kwargsrr
rrZs

zPooledDatabase.initcs zt�|j�\}}|�|�}Wn*tk
rHd}}t�d�Yq�YqX|�||�rxt�d|�d}}|j�	|�q|j
r�|�|�r�t�d|�|�|d�|j�	|�d}}qq�q|dk�r|j
r�t|j�|j
kr�td��tt|�j||�}t��}|�|�}t�d|�||j|<|S)Nz No connection available in pool.zConnection %s was closed.z!Connection %s was stale, closing.TzExceeded maximum connections.zCreated new connection %s.)�heapq�heappoprr�
IndexError�logger�debug�
_is_closedr�discardr�	_is_stale�_closer�lenr�
ValueErrorrr
�_connect�time)r�argsr�ts�conn�keyrr
rr*bs8



�

zPooledDatabase._connectcCst��||jkSr)r+r)rZ	timestampr
r
rr&�szPooledDatabase._is_stalecCs
||jkSr)r)rr/r.r
r
rr$�szPooledDatabase._is_closedFcs�|�|�}|r,|j�|�tt|��|�nh||jkr�|j|}|j|=|jrv|�|�rvt	�
d|�tt|��|�nt	�
d|�t�|j
||f�dS)NzClosing stale connection %s.zReturning %s to pool.)rr�addrr
r'rrr&r"r#r�heappushr)rr.�
close_connr/r-rr
rr'�s


zPooledDatabase._closecCs4|��}|��|�|�|�|�s0|j|dd�dS)zS
        Close the underlying connection without returning it to the pool.
        T�r2N)Zget_conn�closer$rr')rr.r
r
r�manual_close�szPooledDatabase.manual_closecCs"|jD]\}}|j|dd�qdS)z<
        Close all connections managed by the pool.
        Tr3N)rr')r�_r.r
r
r�	close_all�szPooledDatabase.close_all)rN)NN)F)�__name__�
__module__�__qualname__rrr*r&r$r'r5r7�
__classcell__r
r
rrr
Ns,	r
cseZdZ�fdd�Z�ZS)�PooledMySQLDatabasecs:tt|��||�}|s6z|�d�Wnd}YnX|S)NFT)rr<r$Zping)rr/r.Z	is_closedrr
rr$�s
zPooledMySQLDatabase._is_closed�r8r9r:r$r;r
r
rrr<�sr<cseZdZ�fdd�Z�ZS)�_PooledPostgresqlDatabasecs$tt|��||�}|s t|j�}|Sr)rr>r$�bool�closed�rr/r.r@rr
rr$�s
z$_PooledPostgresqlDatabase._is_closedr=r
r
rrr>�sr>c@seZdZdS)�PooledPostgresqlDatabaseN�r8r9r:r
r
r
rrB�srB)�PostgresqlExtDatabasec@seZdZdS)�PooledPostgresqlExtDatabaseNrCr
r
r
rrE�srEcseZdZ�fdd�Z�ZS)�_PooledSqliteDatabasecs4tt|��||�}|s0z
|jWnYdSX|S)NT)rrFr$Z
total_changesrArr
rr$�s
z _PooledSqliteDatabase._is_closedr=r
r
rrrF�srFc@seZdZdS)�PooledSqliteDatabaseNrCr
r
r
rrG�srG)�SqliteExtDatabasec@seZdZdS)�PooledSqliteExtDatabaseNrCr
r
r
rrI�srI)�__doc__rZloggingr+ZpeeweerrrZ	getLoggerr"r�objectr
r<r>rBZplayhouse.postgres_extrDrE�ImportErrorrFrGZplayhouse.sqlite_extrHrIr
r
r
r�<module>s0<
f


Zerion Mini Shell 1.0