How could I check the number of active sqlalchemy connections in a pool at any given time?

陌路散爱 提交于 2020-01-13 09:21:32

问题


I have a situation where sqlalchemy keeps running out of active connections from time to time due to high traffic loads, and I would like to run some tests to verify and optimize the pooling parameters per our use case. However, I cannot find a straightforward way of polling for the count of active connections.

Current setup is on the lines:

args = ...
mapping = {
        'pool_size': 10,
        'max_overflow': 10,
        'pool_timeout': 30,
        'pool_recycle': 1800
    }
engine = sqlalchemy.create_engine(*args, **mapping)

The max connections on the MySQL server is set to 200 and there are about 20 web servers and celery boxes total connecting to it.


回答1:


The default QueuePool has a status method that returns the following:

def status(self):
        return "Pool size: %d  Connections in pool: %d "\
            "Current Overflow: %d Current Checked out "\
            "connections: %d" % (self.size(),
                                 self.checkedin(),
                                 self.overflow(),
                                 self.checkedout())

Pool.checkedout() will return the number of checked out connections.



来源:https://stackoverflow.com/questions/34775501/how-could-i-check-the-number-of-active-sqlalchemy-connections-in-a-pool-at-any-g

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