多进程多线程更新信息

import openpyxl
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor
import time


lis=[['Celery', 6.7],['Garlic',9.4],['Lemon', 2.4]]

# 创建数据更新函数

def update(name, price):
    filename = '/root/Desktop/table1.xlsx'
    wb = openpyxl.load_workbook(filename)
    sheet = wb.active
    try:
        for index, row in enumerate(sheet.rows):
            produceName = sheet.cell(row=index + 1, column=1).value
            if produceName == name:
                sheet.cell(row=index + 1, column=2, value=price)
    except Exception as e:
        print(e)
    else:
        wb.save(filename=filename)


#多线程更新信息
# start = time.time()
# with ThreadPoolExecutor(max_workers=4) as poor:
#     poor.map(update,lis)
#     end = time.time()
#     print('信息更新完成...多线程用时%s' % (end - start))
#

#多进程更新信息
start = time.time()
with ProcessPoolExecutor(max_workers=4) as poor:
    poor.map(update,lis)
    end=time.time()
    print('信息更新完成...多进程用时%s' % (end - start))


#######linux操作系统#########

多线程时:



多进程时:



猜你喜欢

转载自blog.csdn.net/wl_python/article/details/80715085