如何减少使用SQLAlchemy + postgreSQL的连接数量? [英] How to reduce number of connections using SQLAlchemy + postgreSQL?

查看:255
本文介绍了如何减少使用SQLAlchemy + postgreSQL的连接数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Postgres 附加组件开发 heroku ,其开发计划的连接限制为 20 。我是新来的 python 这可能是微不足道的,但我发现很难抽象数据库连接,而不会导致 OperationalError:(OperationalError)致命:现在我有 databeam.py

p>

 从烧瓶导入导入os 
烧瓶
从flask.ext.sqlalchemy导入SQLAlchemy
从设置import databaseSettings

class Db(object):
def __init __(self):
self.app = Flask(__ name__)
self.app.config.from_object( __name__)
self.app.config ['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL',databaseSettings())
self.db = SQLAlchemy(self.app)

db = db()

当我为页面创建控制器时,这样做:
$ b $ pre $ code $ import $ databeam
$ b $ db = databeam.db
locations = databeam。地点

templateVars = d所有()

这确实会产生我想要的,但是会慢慢地导致上面提到的错误。由于我来自 php 背景,我对如何处理数据库连接有一定的心态(即像上面的例子),但我担心它不适合 python



什么是在一个地方抽象数据库连接的正确方法,然后使用相同的连接在所有的进口吗?

解决方案

在SQL Alchemy中,你应该可以创建一个连接池。这个游泳池是每个Dyno游泳池的大小。在开发和基本计划,因为你可以有多达20,你可以设置为20,如果你运行1 dyno,10如果你运行2,等等配置您的池可以设置引擎:



$ p $ engine = create_engine('postgresql:// me @ localhost / mydb',
pool_size = 20,max_overflow = 0)

这会设置您的数据库引擎,其中包含您自动从中抽取的数据库。您也可以手动配置池,更多细节可以在SQL Alchemy的池化指南中找到 - http ://docs.sqlalchemy.org/en/latest/core/pooling.html


I'm developing on heroku using their Postgres add-on with the Dev plan, which has a connection limit of 20. I'm new to python and this may be trivial, but I find it difficult to abstract the database connection without causing OperationalError: (OperationalError) FATAL: too many connections for role.

Currently I have databeam.py:

import os
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from settings import databaseSettings

class Db(object):
    def __init__(self):
        self.app = Flask(__name__)
        self.app.config.from_object(__name__)
        self.app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', databaseSettings())
        self.db = SQLAlchemy(self.app)

db = Db()

And when I'm creating a controller for a page, I do this:

import databeam

db = databeam.db
locations = databeam.locations

templateVars = db.db.session.query(locations).filter(locations.parent == 0).order_by(locations.order.asc()).all()

This does produce what I want, but slowly and at times causes the error metioned above. Since I come from a php background I have a certain mindset of how to deal with DB connections (I.e. like the example above), but I fear it doesn't fit well with python.

What is the proper way of abstracting the db connection in one place and then just using the same connection in all imports?

解决方案

Within SQL Alchemy you should be able to create a connection pool. This pool is what the pool size would be for each Dyno. On the Dev and Basic plan since you could have up to 20, you could set this at 20 if you run 1 dyno, 10 if you run 2, etc. To configure your pool you can setup the engine:

engine = create_engine('postgresql://me@localhost/mydb',
                   pool_size=20, max_overflow=0)

This sets up your db engine with a pool which you pull from automatically then. You can also configure the pool manually, more details on that can be found on the pooling guide of SQL Alchemy - http://docs.sqlalchemy.org/en/latest/core/pooling.html

这篇关于如何减少使用SQLAlchemy + postgreSQL的连接数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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