简体   繁体   English

Flask-SQLAlchemy TimeoutError

[英]Flask-SQLAlchemy TimeoutError

Flask Python 2.7 Postgres 9 Ubuntu 14.04 Flask Python 2.7 Postgres 9 Ubuntu 14.04

I'm using Flask and SQLAlchemy, after 15 consecutive HTTP requests I get: 我正在使用Flask和SQLAlchemy,在连续15次HTTP请求后,我得到:

QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30 大小达到5的QueuePool溢出10的限制,连接超时,超时30

Then server stops responding: 然后服务器停止响应:

Very similar as: 与以下内容非常相似:

Flask-SQLAlchemy TimeoutError Flask-SQLAlchemy TimeoutError

I don't have session engine, per my understanding Flask-SQLAlchemy should take care of it. 我没有会话引擎,据我了解,Flask-SQLAlchemy应该照顾好它。 What do I need to configure to support more sessions and to clean existing ones periodically. 我需要配置什么以支持更多会话并定期清理现有会话。

app.py 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 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

based on @spicyramen comment: 基于@spicyramen评论:

increase SQLALCHEMY_POOL_SIZE The size of the database pool. 增加SQLALCHEMY_POOL_SIZE数据库池的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM