Connection pool
The pool
module contains a number of Database
classes that provide connection pooling for PostgreSQL, MySQL and SQLite databases. The pool works by overriding the methods on the Database
class that open and close connections to the backend. The pool can specify a timeout after which connections are recycled, as well as an upper bound on the number of open connections.
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! If you would like finer-grained control over the pool of connections, check out the advanced_connection_management section.
Pool APIs
class PooledDatabase
(database[, max_connections=20[, stale_timeout=None[, timeout=None[, \*kwargs*]]]])
Parameters: |
|
---|
Mixin class intended to be used with a subclass of Database
.
Note
Connections will not be closed exactly when they exceed their stale_timeout. Instead, stale connections are only closed when a new connection is requested.
Note
If the number of open connections exceeds max_connections, a ValueError will be raised.
_connect
(\args, **kwargs*)Request a connection from the pool. If there are no available connections a new one will be opened.
_close
(conn[, close_conn=False])By default conn will not be closed and instead will be returned to the pool of available connections. If close_conn=True, then conn will be closed and not be returned to the pool.
manual_close
()Close the currently-open connection without returning it to the pool.
class PooledPostgresqlDatabase
Subclass of PostgresqlDatabase
that mixes in the PooledDatabase
helper.
class PooledPostgresqlExtDatabase
Subclass of PostgresqlExtDatabase
that mixes in the PooledDatabase
helper. The PostgresqlExtDatabase
is a part of the Postgresql Extensions module and provides support for many Postgres-specific features.
class PooledMySQLDatabase
Subclass of MySQLDatabase
that mixes in the PooledDatabase
helper.
class PooledSqliteDatabase
Persistent connections for SQLite apps.
class PooledSqliteExtDatabase
Persistent connections for SQLite apps, using the SQLite Extensions advanced database driver SqliteExtDatabase
.