A Flask extension for CuttlePool
Project description
Flask-CuttlePool provides a convenient interface for using Cuttle Pool with Flask.
How-to Guide
If you haven’t read the How-to Guide for CuttlePool, you really should before going any further.
FlaskCuttlePool objects accept the same arguments as CuttlePool objects, as well as a Flask app object. Assume we have the following imports and app object.
import sqlite3 from flask import Flask from flask_cuttlepool import FlaskCuttlePool app = Flask(__name__)
There are two ways to set up a pool object. On pool initialization
pool = FlaskCuttlePool(sqlite3.connect, app=app, database='ricks_lab')
or using init_app() explicitly
pool = FlaskCuttlePool(sqlite3.connect) pool.init_app(app)
Any configuration keys that start with CUTTLEPOOL_ will be converted to a key value pair. If the key already exists in the initial arguments passed to the __init__() method, those will be superceded by the value on app.config. For example
pool = FlaskCuttlePool(sqlite3.connect, database='ricks_lab') app.config['CUTTLEPOOL_DATABASE'] = 'citadel_of_ricks' pool.init_app(app)
will result in the connection pool associated with that app object connecting to 'citadel_of_ricks' instead of 'ricks_lab'. Every key value pair on app.config of the form app.config['CUTTLEPOOL_KEY'] = value is passed to the pool constructor as key=value where key is lowercase.
FlaskCuttlePool objects should also be provided with two callbacks. The ping callback is used to check if a connection is still open. The normalize_connection callback ensures each connection has the same state when it is retrieved from the pool. For more about these methods, see the Cuttle Pool How-to Guide.
Continuing with the above example, these callbacks could be implemented like this:
@pool.ping def ping(connection): try: rv = connection.execute('SELECT 1').fetchall() return (1,) in rv except sqlite3.Error: return False @pool.normalize_connection def normalize_connection(connection): connection.row_factory = None
Now the pool can be used as normal. Any calls to get_connection() will return a connection in the same manner a CuttlePool object would.
To make things more convenient, the connection getter will store a connection on the application context and reuse that connection whenever the connection getter is called again. When the application context is torn down, the connection will be returned to the pool. Therefore, there is no need to call close() on a connection retrieved from the connection getter, but it’s ok if close() is called. Connections retrieved with get_connection() should be explicitly closed.
The convenience method cursor() will return a Cursor instance for the connection stored on the application context.
A full example looks like:
import sqlite3 from flask import Flask from flask_cuttlepool import FlaskCuttlePool app = Flask(__name__) app.config['CUTTLEPOOL_DATABASE'] = ':memory:' pool = FlaskCuttlePool(sqlite3.connect) pool.init_app(app) @pool.ping def ping(connection): try: rv = connection.execute('SELECT 1').fetchall() return (1,) in rv except sqlite3.Error: return False @pool.normalize_connection def normalize_connection(connection): connection.row_factory = None with app.app_context(): # Get a connection, store it on the application context and return to # user. This connection doesn't need to be explicitly closed. con = pool.connection # Subsequent calls to pool.connection will get the same connection from # the application context. con is pool.connection # True # Get a different connection con2 = pool.get_connection() con2 is con # False # This connection should be explicitly closed since it was retrieved by # get_connection(). con2.close() # Get a cursor from the connection on the application context. cur = pool.cursor() cur.execute(SOME_SQL) cur.close() pool.connection.commit() # Now the application context has been torn down, so the connection has been # returned to the pool. pool.connection is None # True
FAQ
These questions are related to Flask-CuttlePool only, check the FAQ for CuttlePool if you don’t find your answers here.
How do I install it?
pip install flask-cuttlepool
What is an application contexts?
This is a Flask extension, so it is meant to be used in the context of a Flask application. See here to learn about Flask’s application context.
Contributing
It’s highly recommended to develop in a virtualenv.
Fork the repository.
Clone the repository:
git clone https://github.com/<your_username>/flask-cuttlepool.git
Install the package in editable mode:
cd flask-cuttlepool pip install -e .[dev]
Now you’re set. See the next section for running tests.
Running the tests
Tests can be run with the command pytest.
Where can I get help?
If you haven’t read the How-to guide above, please do that first. Otherwise, check the issue tracker. Your issue may be addressed there and if it isn’t please file an issue :)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Flask-CuttlePool-0.2.0.tar.gz
.
File metadata
- Download URL: Flask-CuttlePool-0.2.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bd2f8af0f3aeaf4b91b7ad16dcda83cea8fd97e6f14cc1108e91272c174cf00 |
|
MD5 | 72312737fe7c788930ebda0c7d2f813b |
|
BLAKE2b-256 | a32d2ae14e815039f0c45d7e8eb42641c109eb611f77669d6dee24fc636ff682 |
File details
Details for the file Flask_CuttlePool-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: Flask_CuttlePool-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9515a029192790099a0ee4268783b739a7d8aa2edb952d5afb32f3309ff04275 |
|
MD5 | d376b260e8f4f3d0e160825b5f1b04f1 |
|
BLAKE2b-256 | ef4e14407dc2be9802484418ab4f0e7c4066ffdc12f3ad34cbc0895ccfba48df |