使用ThreadPoolExecutor执行任务,来统计显示执行进度,执行耗时 from concurrent . futures import ThreadPoolExecutor , as_completed import time import uuid def vm_one ( uuid , start_time ): time . sleep ( 1 ) global c globa
使用ThreadPoolExecutor执行任务,来统计显示执行进度,执行耗时
from concurrent.futures import ThreadPoolExecutor, as_completedimport time
import uuid
def vm_one(uuid ,start_time):
time.sleep(1)
global c
global total
c = c+1
print("index:{}/{},value:{},time:{:.3f}".format(c,total,uuid,time.time()-start_time))
return uuid
if __name__ == '__main__':
start_time = time.time()
c = 0
uuids = [uuid.uuid4() for _ in range(0,100)]
total = len(uuids)
with ThreadPoolExecutor(max_workers=10) as pool:
all_task = [pool.submit(vm_one, uid,start_time) for uid in uuids]
all_data = []
for out in as_completed(all_task):
mess = out.result()
all_data.append(mess)
输出结果:
E:\myproject\venv\Scripts\python.exe E:/myproject/base_info/convert.pyindex:1/100,value:e6c6cca1-7400-4459-9fc6-59d136577219,time:1.009
index:2/100,value:bc105fb7-69e4-4573-8816-9aa89065045e,time:1.009
..............
index:91/100,value:8fd4858e-84f7-499f-b8db-39a7d33c35d9,time:10.049
index:92/100,value:bc42179e-e16d-4baf-b677-2729da753037,time:10.049
index:93/100,value:a7751a37-0688-43a2-9f95-ee7784c7557d,time:10.049
index:94/100,value:9b37e1bb-1999-48d1-9ffd-ecbda2b26152,time:10.049
index:95/100,value:ba4e321d-0d11-4233-a142-af7ba89aeb60,time:10.049
index:96/100,value:2fd39b41-f7f6-43a7-8953-100514b204d4,time:10.065
index:97/100,value:954b59af-e4af-484a-9964-b3180ceae8a2,time:10.065
index:98/100,value:9a8745de-c3e0-46f6-aa4a-6b8b29d2061e,time:10.065
index:99/100,value:73c9e176-1307-426a-8be6-8296cd87a2df,time:10.065
index:100/100,value:e0d98ebf-9102-42db-a018-da5b2b5d9355,time:10.065
Process finished with exit code 0