首页 > 编程语言 > 详细

threading模块开启多线程

时间:2017-12-20 23:40:01      阅读:219      评论:0      收藏:0      [点我收藏+]
 1 import threading
 2 from time import ctime,sleep
 3 loops = [4,2]
 4 def loop(nloop,nsec):
 5     print(start loop,nloop,at:,ctime())
 6     sleep(nsec)
 7     print(loop,nloop,end at:,ctime())
 8 def main():
 9     print(starting at:,ctime())
10     threads = []
11     nloops = range(len(loops))
12 
13     for i in nloops:
14         t = threading.Thread(target=loop,args=(i,loops[i]))
15         threads.append(t)           #进程准备
16     for i in nloops:        
17         threads[i].start()          #进程开始
18     for i in nloops:               #等待进程结束
19         threads[i].join()           #关闭进程
20     print(all done at ,ctime())
21 
22 if __name__ == __main__:
23     main()

 

threading模块开启多线程

原文:http://www.cnblogs.com/leescre/p/8076270.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!