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)

  

posted @ 2020-12-04 09:29  没有显示名称  阅读(952)  评论(0编辑  收藏  举报