日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

python操作mysql(二)

發(fā)布時(shí)間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。