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)