Python flask-SQLAlchemy query time out error

。_饼干妹妹 提交于 2019-12-11 05:56:31

问题


I am using flask-sqlalchemy library in python to management my database(my sql). However when I test query, it give me this error:

sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 10

Test code is simple:

 def test_query(self, i):
        random = str(i)
        result = Dao.query_user_by_nick("kael")
        print("end query at %s, id: %s" % (datetime.datetime.now(), random), "\n result: " + str(len(result)))



threads = []
    for i in range(0,100):
        test = threading.Thread(target=test_query, args=(self, str(i)))
        threads.append(test)
    for t in threads:
        t.start()

The implement of query_user_by_nick is:

@classmethod
def query_user_by_nick(self,name):
    user = User.query.filter_by(nick_name=name).all()
    return user

When I running the query in multi-thread, this error will always always happen, and I have no idea why it will be. I also insert some data in multi-thread and it works fine. I searched around and nothing inspired me. Someone said the connection will be limited to 15, but this database will be query very frequently and can not be just like 15 query at some time or similar. I am happy if anyone could give me any suggestion. Thanks in advance.


回答1:


If you think your database can handle it, you should be able to increase the SQLAlchemy pool settings via http://flask-sqlalchemy.pocoo.org/2.1/config/ :

  • SQLALCHEMY_MAX_OVERFLOW can be increased (from 10) to the max number of concurrent connections you want to allow
  • SQLALCHEMY_POOL_TIMEOUT can be increased to give more time for a waiting thread to get a connection from the pool if response time is not a major concern


来源:https://stackoverflow.com/questions/43936226/python-flask-sqlalchemy-query-time-out-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!