Ebook123

导航

Python Threading 模块计算所有线程的执行时间

import threading
import time

def run(n):
print("task",n)
time.sleep(1)

t_objs = []
start_time = time.time()
for i in range(1,6):
t = threading.Thread(target=run,args=("t-%s" %i,))
t.start()
t_objs.append(t)

for t in t_objs:
t.join()

print("-----all threads has finished-----")
print("cost:",time.time() - start_time)

posted on 2021-04-09 22:09  Ebook123  阅读(805)  评论(0编辑  收藏  举报