snower / TorMySQL

The highest performance asynchronous MySQL driver by PyMySQL
MIT License
308 stars 63 forks source link

采用连接池频繁获取连接释放连接,对系统性能消耗大吗? #33

Closed fatpo closed 6 years ago

fatpo commented 6 years ago

因为我的项目中,每一个DAO操作都要来一遍

with (yield pool.Connection()) as conn:
    my_code

想知道频繁的获取连接、释放连接,会不会给性能带来巨大消耗?

假设我的配置如下:

pool = tormysql.ConnectionPool(
    max_connections = 20, #max open connections
    idle_seconds = 7200, #conntion idle timeout time, 0 is not timeout
    wait_connection_timeout = 3, #wait connection timeout
    host = "127.0.0.1",
    user = "root",
    passwd = "TEST",
    db = "test",
    charset = "utf8"
)

@gen.coroutine
def test():
    with (yield pool.Connection()) as conn:
        try:
            with conn.cursor() as cursor:
                yield cursor.execute("INSERT INTO test(id) VALUES(1)")
        except:
            yield conn.rollback()
        else:
            yield conn.commit()

        with conn.cursor() as cursor:
            yield cursor.execute("SELECT * FROM test")
            datas = cursor.fetchall()
snower commented 6 years ago

并不会的,之所有要用连接池就是要尽可能节省系统资源的,获取和释放连接其实内部就是一个collections.deque append和pop连接实例的过程,并不是设计连接操作,你可以测试一下collections.deque的性能看看,很快的

而且长时间持有连接而不是使用完就尽快把连接放入连接池,那么tornado高并发的下就只能多开很多连接来支持操作,这样多tornado和mysql都会消耗更多的资源来管理连接

fatpo commented 6 years ago

明白了。理论上我们的宗旨是用完就放回池子里,不要hold住连接。 thx。