python多线程查询数据库并获取返回结果

更新时间:2023-05-12 02:37:43 阅读: 评论:0

python多线程查询数据库并获取返回结果pip install DBUtils==1.3
pip install mysqlclient==2.0.1
import time
import threading
import MySQLdb
import queue
from MySQLdb.cursors import DictCursor
from DBUtils.PooledDB import PooledDB
def mysql_connection():
host = 'host'
ur = 'ur'
port = 3306
password = 'pwd'
db = 'mysql'
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():
con = tion()
cur = con.cursor()
sql = """
lect * from ....
"""
result = cur.fetchall()
con.clo()
return result
class MyThread(threading.Thread):
def __init__(lf, func, args):
super(MyThread, lf).__init__()
lf.func = func
lf.args = args
def run(lf):
def get_result(lf):
try:
sult
except Exception:
return None
if __name__ == '__main__':
start = time.time()
# 创建线程连接池
pool = mysql_connection()
# 创建队列,队列的最⼤个数及限制线程个数
q = queue.Queue(maxsize=12)
# 测试数据,多线程查询数据库
for i in range(12):
# 创建线程并放⼊队列中
# t = MyThread(target=tread_connection_db, args=(id,))
t = MyThread(tread_connection_db, args=(i,))
q.put(t)
# 队列队满
if q.qsize() == 12:
# ⽤于记录线程,便于终⽌线程
join_thread = []
# 从对列取出线程并开始线程,直到队列为空
pty() != True:
t = q.get()
join_thread.append(t)
t.start()
# 终⽌上⼀次队满时⾥⾯的所有线程
for t in join_thread:
t.join()
for t in join_thread:                _result())
end = time.time() - start    print(end)

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

本文链接:https://www.wtabcd.cn/fanwen/fan/82/593649.html

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

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