SQLAlchemy / Flask / PostgreSQL池连接 [英] SQLAlchemy / Flask / PostgreSQL pool connection

查看:204
本文介绍了SQLAlchemy / Flask / PostgreSQL池连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在和Django玩了很长时间之后,我用SQLAlchemy尝试了一下Flask,我必须说我非常喜欢它。但是有一些我不明白:
我有一个使用PostgreSQL的Flask / SQLAlchemy应用程序。
在我的 __ init __。py 文件中,我有:

pre $ from flask进口烧瓶
from flask.ext.sqlalchemy import SQLAlchemy
$ b $ app = Flask(__ name__)
app.config.from_object('settings')
db = SQLAlchemy(app)

我想知道:


  1. 是否有自动连接池?还是每次调用Model.query ...都会创建一个新的数据库连接?

  2. 如果不是,我该如何配置它?

  3. 如果是,那么默认值是什么,如何修改这些值?

非常感谢您的帮助!

解决方案<在SQLAlchemy中,Flask-SQLAlchemy使用 create_engine 方法创建一个SQLAlchemy引擎,你可以在这里读到一些选项和缺省值。文档根据Flask-SQLAlchemy文档,您可以指定一些特定于池的配置选项。您可以通过各种方式设置这些值,您可以在 Flask配置中阅读这些值。你已经有一个设置配置模块,所以你可以添加到你的配置文件像...



<$ p $所以,是的,有自动连接池。这是一个自动连接池。 默认情况下由SQLAlchemy提供。在发布这个答案时,Flask-SQLAlchemy允许你使用配置文件修改这些选项中的一些<尽管看起来有一个旧的 pull request让你指定任何create_engine参数)。

如果你需要更多的配置支持比Flask-SQLAlchemy提供的SQLAlchemy引擎,你可以使用没有Flask-SQLAlchemy包装器的SQLAlchemy,或者修改Flask-SQLAlchemy(也许合并pull请求)来允许这个。


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:

  1. Is there automatic connection pooling ? Or does every call to Model.query... create a new connection to the database?
  2. If not, how can I configure it?
  3. If yes, what are the defaults, how can I modify the values?

Thank you very much for you help !

解决方案

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.

这篇关于SQLAlchemy / Flask / PostgreSQL池连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆