python操作mysql(二)
生活随笔
收集整理的這篇文章主要介紹了
python操作mysql(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
使用方法:
In [1]: from DBUtils.PooledDB import PooledDB //導入PooledDB這個類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 //需要一個DB-API模塊,返回一個新的DB-API| connection objects or a DB-API 2 compliant database module| mincached: initial number of idle connections in the pool //最小空閑連接數,如果空閑連接數少于這個數,pool會創建一個新的連接| (0 means no connections are made at startup)| maxcached: maximum number of idle connections in the pool //最大空閑連接數,如果空閑連接數大于這個數,pool會關閉空閑連接| (0 or None means unlimited pool size)| maxconnections: maximum number of connections generally allowed //最大連接數| (0 or None means an arbitrary number of connections)| blocking: determines behavior when exceeding the maximum //當連接數達到maxconnections時,如果blocking=True,應用程序會一直等待,直到當前連接數少于最大連接數| (if this is set to true, block and wait until the number of // 如果blocking=False,連接池會報告錯誤| connections decreases, otherwise an error will be reported).............In [7]: pool = PooledDB(creator=MySQLdb, mincached=5, maxcached=10, maxconnections=20, **config) //實例化獲得連接池對象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中,每個http請求都會分發給一個進程,連接池中配置的連接數都是一個進程為單位的(即上面的最大連接數,都是在一個進程中的連接數),如果業務中,一個http請求中需要的sql連接數不是很多的話(其實大多數都只需要創建一個連接),連接數配置都不需要太大 連接池對性能的提升表現在:- 在程序創建連接的時候,可以直接使用一個空閑連接,不需要重新初始化連接,提升獲取連接的速度
- 關閉連接的時候,把連接放回連接池,而不是真正的關閉,所以可以減少頻繁地打開和關閉連接的操作
轉載于:https://www.cnblogs.com/tobeone/p/7877521.html
總結
以上是生活随笔為你收集整理的python操作mysql(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 风凰香烟多少钱一条
- 下一篇: WebSocket的几个模块(node.