Python学习之路:进程、线程、协程篇

参考链接:http://www.cnblogs.com/alex3714/articles/5230609.html

 1 import threading,time
 2 
 3 def run(n):
 4     print("task",n)
 5     time.sleep(2)
 6     print("task done",n)
 7 
 8 start_time = time.time()
 9 t_objs =  []
10 for i in range(50):
11     t = threading.Thread(target=run,args=("t-%s"%i,))
12     t.start()
13     t_objs.append(t)
14 
15 for t in t_objs:
16     t.join()
17 
18 print("------all threads has finished....")
19 print("cost:",time.time()-start_time)
20 
21     # t1 = threading.Thread(target=run,args=("t1",))
22     # t2 = threading.Thread(target=run,args=("t2",))
23     # t1.start()
24     # t2.start()
25 
26 # run("t1")
27 # run("t2")
多线程并计时

 

posted @ 2018-03-15 18:20  Py小白  阅读(150)  评论(0编辑  收藏  举报