python使用多线程查询数据库

更新时间:2023-05-12 02:27:18 阅读: 评论:0

python使⽤多线程查询数据库
⼀.背景:
当数据量过⼤时,⼀个程序的执⾏时间就会主要花费在等待单次查询返回结果,在这个过程中cpu⽆疑是处于等待io的空闲状态的,这样既浪费了cpu资源,⼜花费了⼤量时间(当然这⾥主要说多线程,批量查询不在考虑范围,总会存在不能批量查询的情况),在这种⾮密集型运算(及⼤量占⽤cpu资源)的情况下在python中⽆疑运⽤多线程是⼀个⾮常棒的选择。
⼆.知识点:
数据库连接池的运⽤及优势,python中多线程的运⽤,队列的运⽤
数据库连接池:限制了数据库的连接最⼤个数,每次连接都是可以重复使⽤的,当然也可以限制每个连接的重复使⽤次数(这个在这⾥是没必要的),需要注意的是设置的数据库的最⼤连接个数最好要⼤于我们⾃⼰开的最⼤线程个数,⼀般逻辑是每个线程占⽤⼀个数据库连接可以使程序达到最⼤速度,如果⼩于则可能存在同时连接个数⼤于数据库允许的最⼤连接个数的风险。使⽤数据库连接池的优势在
于,python多线程并发操作数据库,会存在链接数据库超时、数据库连接丢失、数据库操作超时等问题,⽽数据库连接池提供线程间可共享的数据库连接,并⾃动管理连接。
python多线程:在程序等待io的时间⾥调⽤多线程去数据库执⾏查询操作。
队列:这个就是数据结构⾥⾯的知识了,⼀般队列的常⽤模式先进先出队列。(这⾥主要⽤的是队列取⼀个数就少⼀个数的原理,其实⽤列表也可以实现,他的先进先出主要强调的是⼀个顺序关系,这⼀点到没⽤上,就当是练练⼿了)
三.两段代码作⽐较:
数据库的截图:
第⼀段代码:正常循环查询并打印出执⾏时间
import MySQLdb
import Queue
from MySQLdb.cursors import DictCursor
from DBUtils.PooledDB import PooledDB
def mysql_connection():
host = 'localhost'
ur = 'root'
port = 3306
password = '123456'
db = 'test'
chart = 'utf8'
limit_count = 3  # 最低预启动数据库连接数量
pool = PooledDB(MySQLdb, limit_count, maxconnections=15, host=host, ur=ur, port=port, passwd=password, db=db, chart=chart,            u_unicode=True, cursorclass=DictCursor)
return pool
start = time.time()
pool = mysql_connection()
for id in range(50):
con = tion()
cur = con.cursor()
sql = '''lect id,name,age,weight from test where id = %s '''%id
time.sleep(0.5)
result = cur.fetchall()
if result:
print('this is tread %s (%s,%s,%s,%s)'%(id,result[0]['id'],result[0]['name'],result[0]['age'],result[0]['weight']))
el:
print('this tread %s result is none'%id)
end = time.time() - start
print(end)
执⾏结果:
第⼆段代码:限制数据库连接池最⼤15个连接,⽤队列限制最⼤线程个数为10个
import MySQLdb
import Queue
from MySQLdb.cursors import DictCursor
from DBUtils.PooledDB import PooledDB
def mysql_connection():
host = 'localhost'
ur = 'root'
port = 3306
password = '123456'
db = 'test'
chart = 'utf8'
limit_count = 3  # 最低预启动数据库连接数量
pool = PooledDB(MySQLdb, limit_count, maxconnections=15, host=host, ur=ur, port=port, passwd=password, db=db, chart=chart,            u_unicode=True, cursorclass=DictCursor)
return pool
def tread_connection_db(id):
con = tion()
cur = con.cursor()
sql = '''lect id,name,age,weight from test where id = %s '''%id
time.sleep(0.5)
result = cur.fetchall()
if result:
print('this is tread %s (%s,%s,%s,%s)'%(id,result[0]['id'],result[0]['name'],result[0]['age'],result[0]['weight']))
el:
print('this tread %s result is none'%id)
con.clo()
if __name__=='__main__':
start = time.time()
#创建线程连接池,最⼤限制15个连接
pool = mysql_connection()
#创建队列,队列的最⼤个数及限制线程个数
q=Queue.Queue(maxsize=10)
#测试数据,多线程查询数据库
for id in range(50):
#创建线程并放⼊队列中
t = threading.Thread(target=tread_connection_db, args=(id,))
q.put(t)
#队列队满
if q.qsize()==10:
#⽤于记录线程,便于终⽌线程
join_thread = []
#从对列取出线程并开始线程,直到队列为空
pty()!=True:
t = q.get()
join_thread.append(t)
t.start()
#终⽌上⼀次队满时⾥⾯的所有线程
for t in join_thread:
t.join()
end = time.time() - start
print(end)
程序备注应该还算⽐较清晰的哈,程序执⾏结果:
四.结论:
看结果说话

本文发布于:2023-05-12 02:27:18,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/885910.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据库   连接   线程   个数   查询   队列   程序   等待
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图