python DBUtils.PooledDB 中 maxcached 和 maxconnections

來源:互聯網
上載者:User

標籤:

PooledDB 有這麼幾個參數

  • mincached : the initial number of idle connections in the pool (the default of 0 means no connections are made at startup)

  • maxcached: the maximum number of idle connections in the pool (the default value of 0 or None means unlimited pool size)

  • maxconnections: maximum number of connections generally allowed (the default value of 0 or None means any number of connections)

  • blocking: determines behavior when exceeding the maximum

其中 maxconnections 的描述有點奇怪,它說 generally allowed,為什麼是 generally ?

設定 blocking=True,使用 MySQLdb 實驗的結果如下:

1. 如果 maxconnections 參數不存在,那麼串連數可以無限大,直至打滿 mysql

2. 如果 maxcached < maxconnections,那麼最大串連數就是 maxconnections, 但是奇怪的是,並不是所有 connections 都被複用了,pool 還是會建立新的串連

3. 如果 maxcached > maxconnections,那麼最大串連數就是 maxcached, 同樣存在 connections 可以複用但是還是會建立新串連的問題

 

測試代碼

from threading import Threadfrom DBUtils.PooledDB import PooledDBfrom datetime import datetimeimport MySQLdbpool = PooledDB(creator=MySQLdb,                         mincached=1,                         maxcached=3,                         maxconnections=3,                         blocking=True,                         user="test",                         passwd="test",                         db="test")def test1(pool):    print("start: %s" % datetime.now())    conn = pool.connection()    print conn    cursor = conn.cursor()    print("select: %s" % datetime.now())    cursor.execute("select sleep(10)")    cursor.close()    conn.close()    print("end: %s" % datetime.now())def main():    for i in xrange(15):        Thread(target=test1, args=(pool,)).start()if __name__ == "__main__":    main()

 

python DBUtils.PooledDB 中 maxcached 和 maxconnections

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.