After having played for a long time with Django, I'm trying a bit of Flask with SQLAlchemy, and I must say I quite like it. However there is something that I don't get:
I have a small Flask / SQLAlchemy app that uses PostgreSQL.
In my __init__.py
file I have:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('settings')
db = SQLAlchemy(app)
I wanted to know:
Thank you very much for you help !
SQLAlchemy includes several connection pool implementations which integrate with the Engine . They can also be used directly for applications that want to add pooling to an otherwise plain DBAPI approach.
Step 1 - Install the Flask-SQLAlchemy extension. Step 2 - You need to import the SQLAlchemy class from this module. Step 3 - Now create a Flask application object and set the URI for the database to use. Step 4 - then use the application object as a parameter to create an object of class SQLAlchemy.
connect() method returns a Connection object, and by using it in a Python context manager (e.g. the with: statement) the Connection. close() method is automatically invoked at the end of the block.
Every pool implementation in SQLAlchemy is thread safe, including the default QueuePool . This means that 2 threads requesting a connection simultaneously will checkout 2 different connections. By extension, an engine will also be thread-safe.
Flask-SQLAlchemy creates a SQLAlchemy engine using the create_engine
method in SQLAlchemy, which you can read about some of the options and defaults in the documentation for the create_engine function. According to the Flask-SQLAlchemy documentation, you can specify some of the configuration options specific to pooling. You can set those values in various ways, which you can read about in the Flask configuration. You already have a settings
configuration module, so you can add to your config file something like...
SQLALCHEMY_POOL_SIZE=10
So, yes, there is automatic connection pooling. This is provided by SQLAlchemy by default. As of the posting of this answer, Flask-SQLAlchemy allows you to modify some of these options using the configuration file (although it appears there is an old pull request to allow you to specify ANY create_engine parameter).
If you need more support for configuring the SQLAlchemy engine than Flask-SQLAlchemy provides, you can either use SQLAlchemy without the Flask-SQLAlchemy wrapper, or modify Flask-SQLAlchemy (perhaps merging the pull request) to allow this.
Every worker of your flask instance running a proper wsgi server with multiple workers will have it's own engine - because your script is loaded for every worker separately.
In consequence you have simply no control about the real pooling, except you limit your worker threads of wsgi server and calculate the max allowed connections per engine/pool instance.
Further details can be read at https://docs.sqlalchemy.org/en/13/core/pooling.html#using-connection-pools-with-multiprocessing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With