Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-CuttlePool-0.2.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

Flask_CuttlePool-0.2.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file Flask-CuttlePool-0.2.1.tar.gz.

File metadata

  • Download URL: Flask-CuttlePool-0.2.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3

File hashes

Hashes for Flask-CuttlePool-0.2.1.tar.gz
Algorithm Hash digest
SHA256 af62c1bbeb3e5c39a040fa4e55ee7a48a439293afa53fe530a09f43091496fc7
MD5 1f504f4ea77f698fca04753d228a76a3
BLAKE2b-256 b8af228495e8f36bd52648af2958e8b43c07187c23ca14eff3ad4cdc65594e92

See more details on using hashes here.

File details

Details for the file Flask_CuttlePool-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: Flask_CuttlePool-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3

File hashes

Hashes for Flask_CuttlePool-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 31640a257dedcb2bc61f329a0dc451c1d81abb07347a251b3aacc009125d661a
MD5 98ab1a1a79dbc3ed383f3b7409bc1d03
BLAKE2b-256 0b483bd66db1a6f8bc025cbf364bf31e4a9eb19af01c9cc10fe4638789ab3bff

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page