作业描述:使用进程去读取execl列表中的数据,然后用线程写入数据库
from MySQL import MySQl
from config import mysql_info
import openpyxl
import threading
import multiprocessing
import threadpool,os
lock=threading.Lock()
mysql_object=MySQl(mysql_info)
class InsertData:
data_path="./data"
# 获取数据
@staticmethod
def get_data(file_name):
book=openpyxl.load_workbook(file_name)
sheet=book.active
data_list=[]
for row in sheet.rows:
row_data=[col.value for col in row]
data_list.append(row_data)
return data_list
# 执行sql语句
def run(self,data):
numbers, string =data
sql="insert into lxp_thread(num,string) values('%s','%s')"%(numbers,string)
with lock:
mysql_object.execute(sql)
# 线程池运行
def to_db(self,data_list):
pool=threadpool.ThreadPool(10)
reqs=threadpool.makeRequests(self.run,data_list)
for req in reqs:
pool.putRequest(req)
pool.wait()
# 主函数
def main(self):
pool=multiprocessing.Pool(5)
for file in os.listdir(self.data_path):
abs_path=os.path.join(self.data_path,file)
data_list=self.get_data(abs_path)
pool.apply_async(self.to_db,args=(data_list,))
pool.close()
pool.join()
print("done!")
#
if __name__ == '__main__':
my=InsertData()
my.main()