简体   繁体   中英

Flask-SQLAlchemy TimeoutError

Flask Python 2.7 Postgres 9 Ubuntu 14.04

I'm using Flask and SQLAlchemy, after 15 consecutive HTTP requests I get:

QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30

Then server stops responding:

Very similar as:

Flask-SQLAlchemy TimeoutError

I don't have session engine, per my understanding Flask-SQLAlchemy should take care of it. What do I need to configure to support more sessions and to clean existing ones periodically.

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

based on @spicyramen comment:

increase SQLALCHEMY_POOL_SIZE The size of the database pool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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