Flask-SQLAlchemy TimeoutError

时间:2015-11-03 06:42:05

标签: python postgresql flask flask-sqlalchemy

烧瓶 Python 2.7 Postgres 9 Ubuntu 14.04

我使用Flask和SQLAlchemy,经过15次连续的HTTP请求后,我得到了:

QueuePool限制大小5溢出10达到,连接超时,超时30

然后服务器停止响应:

非常相似:

Flask-SQLAlchemy TimeoutError

我没有会话引擎,根据我的理解,Flask-SQLAlchemy应该照顾它。 我需要配置什么才能支持更多会话并定期清理现有会话。

app.py

import models
api_app = Flask(__name__)
api_app.config.from_pyfile(settings.celery_config)
db = SQLAlchemy(api_app)


@api_app.teardown_appcontext
def shutdown_session(exception=None):
    try:
        db.session.close()
    except AttributeError,e:
        print str(e)
    except Exception,e:
        print api_app.name
        print str(e)

@api_app.route('/api/1.0/job/<string:ref>/')
def get_job_by_id(ref):
    """

    :param id:
    :return:
    """
    try:
        if request.method == 'GET':
            job = models.Job.query.filter(models.Job.reference == ref)
            if job:
                job_ = job.all()
                if len(job_) == 1:
                    return jsonify(job_[0].serialize())

            resp = Response(status=404, mimetype='application/json')
            return resp            

        else:
            resp = Response(status=405, mimetype='application/json')
            return resp
    except Exception,e:
        print str(e)
        resp = Response(status=500, mimetype='application/json')
        return resp

models.py

from api_app import db
class Job(db.Model, AutoSerialize, Serializer):
    __tablename__ = 'job'
    __public__ = ('status','description','reference')
    id = Column(Integer, primary_key=True, server_default=text("nextval('job_id_seq'::regclass)"))
    status = Column(String(40), nullable=False)
    description = Column(String(200))
    reference = Column(String(50))

    def serialize(self):
        d = Serializer.serialize(self)
        del d['id']
        return d

1 个答案:

答案 0 :(得分:1)

基于@spicyramen评论:

增加SQLALCHEMY_POOL_SIZE数据库池的大小。