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 its 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 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.
manual_close
()Close the currently-open connection without returning it to the pool.
close_idle
()Close all idle connections. This does not include any connections that are currently in-use – only those that were previously created but have since been returned back to the pool.
close_stale
([age=600])Parameters: age (int) – Age at which a connection should be considered stale. Returns: Number of connections closed. Close connections which are in-use but exceed the given age. Use caution when calling this method!
close_all
()Close all connections. This includes any connections that may be in use at the time. Use caution when calling this method!
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
.