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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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'
user = 'user'
port = 3306
password = 'pwd'
db = 'mysql'
charset = 'utf8'
limit_count = 3 # 最低预启动数据库连接数量
pool = PooledDB(MySQLdb, limit_count, maxconnections=15, host=host, user=user, port=port, passwd=password, db=db,
charset=charset,
use_unicode=True, cursorclass=DictCursor)
return pool
def tread_connection_db():
con = pool.connection()
cur = con.cursor()
sql = """
select * from ....
"""
cur.execute(sql)
result = cur.fetchall()
con.close()
return result
class MyThread(threading.Thread):
def __init__(self, func, args):
super(MyThread, self).__init__()
self.func = func
self.args = args
def run(self):
self.result = self.func(*self.args)
def get_result(self):
try:
return self.result
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 = []
# 从对列取出线程并开始线程,直到队列为空
while q.empty() != True:
t = q.get()
join_thread.append(t)
t.start()
# 终⽌上⼀次队满时⾥⾯的所有线程
for t in join_thread:
t.join()
for t in join_thread: print(t.get_result())
end = time.time() - start print(end)。

相关文档
最新文档