多线程版本1
def process_row_thread1(row):
print(get_maturity(row[0]))
def detail_excel_file_thread1(excel_path):
workbook = openpyxl.load_workbook(excel_path)
sheet = workbook['Sheet']
rows = list(sheet.iter_rows(values_only=True))
rows = rows[1:]
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(process_row_thread1, row) for row in rows]
for future in as_completed(futures):
try:
future.result()
except Exception as exc:
print(f'{exc}')
print('成熟度编辑完成')
workbook.save(excel_path)
多线程版本2
def process_row_thread2(sheet,row,row_index):
value = row[0]
if value is not None and row_index>0:
print(get_maturity(value))
print(f'{value}处理完毕')
def detail_excel_file_thread2(excel_path):
workbook = openpyxl.load_workbook(excel_path)
sheet = workbook.active
rows = list(sheet.iter_rows(values_only=True))
threads = []
for row_index, row in enumerate(rows):
t = threading.Thread(target=process_row_thread2, args=(sheet,row,row_index))
threads.append(t)
t.start()
for t in threads:
t.join()
print('成熟度编辑完成')
workbook.save(excel_path)