python操作mysql(二)
生活随笔
收集整理的這篇文章主要介紹了
python操作mysql(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
使用方法:
In [1]: from DBUtils.PooledDB import PooledDB //導(dǎo)入PooledDB這個(gè)類In [3]: help(PooledDB)
Help on class PooledDB in module DBUtils.PooledDB:class PooledDB
....| | __init__(self, creator, mincached=0, maxcached=0, maxshared=0, maxconnections=0, blocking=False, maxusage=None, setsession=None, reset=True, failures=None, ping=1, *args, **kwargs)| Set up the DB-API 2 connection pool.| | creator: either an arbitrary function returning new DB-API 2 //需要一個(gè)DB-API模塊,返回一個(gè)新的DB-API| connection objects or a DB-API 2 compliant database module| mincached: initial number of idle connections in the pool //最小空閑連接數(shù),如果空閑連接數(shù)少于這個(gè)數(shù),pool會(huì)創(chuàng)建一個(gè)新的連接| (0 means no connections are made at startup)| maxcached: maximum number of idle connections in the pool //最大空閑連接數(shù),如果空閑連接數(shù)大于這個(gè)數(shù),pool會(huì)關(guān)閉空閑連接| (0 or None means unlimited pool size)| maxconnections: maximum number of connections generally allowed //最大連接數(shù)| (0 or None means an arbitrary number of connections)| blocking: determines behavior when exceeding the maximum //當(dāng)連接數(shù)達(dá)到maxconnections時(shí),如果blocking=True,應(yīng)用程序會(huì)一直等待,直到當(dāng)前連接數(shù)少于最大連接數(shù)| (if this is set to true, block and wait until the number of // 如果blocking=False,連接池會(huì)報(bào)告錯(cuò)誤| connections decreases, otherwise an error will be reported).............In [7]: pool = PooledDB(creator=MySQLdb, mincached=5, maxcached=10, maxconnections=20, **config) //實(shí)例化獲得連接池對(duì)象In [8]: help(PooledDB.connection)
Help on method connection in module DBUtils.PooledDB:connection(self, shareable=True) unbound DBUtils.PooledDB.PooledDB methodGet a steady, cached DB-API 2 connection from the pool.If shareable is set and the underlying DB-API 2 allows it,then the connection may be shared with other threadsIn [9]: conn = pool.connection() //在連接池中獲得連接
?
在uwsgi中,每個(gè)http請(qǐng)求都會(huì)分發(fā)給一個(gè)進(jìn)程,連接池中配置的連接數(shù)都是一個(gè)進(jìn)程為單位的(即上面的最大連接數(shù),都是在一個(gè)進(jìn)程中的連接數(shù)),如果業(yè)務(wù)中,一個(gè)http請(qǐng)求中需要的sql連接數(shù)不是很多的話(其實(shí)大多數(shù)都只需要?jiǎng)?chuàng)建一個(gè)連接),連接數(shù)配置都不需要太大 連接池對(duì)性能的提升表現(xiàn)在:- 在程序創(chuàng)建連接的時(shí)候,可以直接使用一個(gè)空閑連接,不需要重新初始化連接,提升獲取連接的速度
- 關(guān)閉連接的時(shí)候,把連接放回連接池,而不是真正的關(guān)閉,所以可以減少頻繁地打開和關(guān)閉連接的操作
轉(zhuǎn)載于:https://www.cnblogs.com/tobeone/p/7877521.html
總結(jié)
以上是生活随笔為你收集整理的python操作mysql(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 风凰香烟多少钱一条
- 下一篇: WebSocket的几个模块(node.