新聞中心
下面的內(nèi)容主要是介紹Python數(shù)據(jù)庫連接池應(yīng)用于多線程環(huán)境中使用的具體方案的具體介紹。如果你對(duì)Python數(shù)據(jù)庫連接池在其具體方案應(yīng)用的相關(guān)步驟感興趣的話,你就可以點(diǎn)擊以下的文章,對(duì)其進(jìn)行了解。

“專業(yè)、務(wù)實(shí)、高效、創(chuàng)新、把客戶的事當(dāng)成自己的事”是我們每一個(gè)人一直以來堅(jiān)持追求的企業(yè)文化。 成都創(chuàng)新互聯(lián)是您可以信賴的網(wǎng)站建設(shè)服務(wù)商、專業(yè)的互聯(lián)網(wǎng)服務(wù)提供商! 專注于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、軟件開發(fā)、設(shè)計(jì)服務(wù)業(yè)務(wù)。我們始終堅(jiān)持以客戶需求為導(dǎo)向,結(jié)合用戶體驗(yàn)與視覺傳達(dá),提供有針對(duì)性的項(xiàng)目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場,引領(lǐng)市場!
示例:
- #-*-coding:utf-8-*-
- import threading,time,datetime
- import MySQLdb
- from DBUtils import PooledDB
- pool = PooledDB.PooledDB(MySQLdb,100,50,100,490,False,
host='localhost',user='root',passwd='321',db='test',
charset='utf8')
默認(rèn)打開的時(shí)候就創(chuàng)建了100個(gè)數(shù)據(jù)庫連接。檢查發(fā)現(xiàn)果然數(shù)據(jù)庫中有100個(gè)
- class MyThread(threading.Thread):
- def __init__(self,threadName):
- self.conn = pool.connection()
直接從數(shù)據(jù)庫連接池中提取
- threading.Thread.__init__(self,name=threadName)
- def run(self):
- cursor=self.conn.cursor()
- print "hello--->",self.getName()
- file_objct = open('8.txt','a+')
- file_objct.write(self.getName()+'\n')
- file_objct.close()
- #cursor.execute("call loaddate();")
- #self.conn.commit()
- time.sleep(10)
- def __del__(self):
- self.conn.close()
- self.conn = None
- for i in range(5):
- obj = MyThread(str(i))
- obj.start()
如果我開480個(gè)線程的話 數(shù)據(jù)庫顯示的正是480個(gè)連接!maxconnections: ***允許連接數(shù)量(缺省值 0 代表不限制)如果我現(xiàn)在將代碼調(diào)整如下:
- #-*-coding:utf-8-*-
- import threading,time,datetime
- import MySQLdb
- from DBUtils import PooledDB
- pool = PooledDB.PooledDB(MySQLdb,100,50,100,400,False,
host='localhost',user='root',passwd='321',db='test',
charset='utf8')- class MyThread(threading.Thread):
- def __init__(self,threadName):
- self.conn = pool.connection()
- threading.Thread.__init__(self,name=threadName)
- def run(self):
- cursor=self.conn.cursor()
- print "hello--->",self.getName()
- file_objct = open('8.txt','a+')
- file_objct.write(self.getName()+'\n')
- file_objct.close()
- #cursor.execute("call loaddate();")
- #self.conn.commit()
- time.sleep(10)
- def __del__(self):
- self.conn.close()
- self.conn = None
- for i in range(402):
- obj = MyThread(str(i))
- obj.start()
連接池***的數(shù)目才400 。
網(wǎng)站名稱:Python數(shù)據(jù)庫連接池相關(guān)示例詳細(xì)介紹
本文路徑:http://www.dlmjj.cn/article/dppehcs.html


咨詢
建站咨詢
