threading模块开启多线程

 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()
原文地址:https://www.cnblogs.com/leescre/p/8076270.html