標籤:
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