首页 > 代码库 > Python模块学习------ 多线程

Python模块学习------ 多线程

# 创建一个Thread实例,传给它一个函数;

import threading from time import sleep, ctime loops = [4,2] def loop(nloop, nsec): print "start loop", nloop, "at:",ctime() sleep(nsec) print "end loop", nloop, "at:",ctime() def main(): print "*****start*********", ctime() threads = [] nloops = range(len(loops)) for i in nloops: t = threading.Thread(target=loop, args=(i, loops[i])) threads.append(t) # start threads for i in nloops: threads[i].start() # wait for all threads to finish for i in nloops: threads[i].join() print "*******end*******", ctime() if __name__ == "__main__": main()

 

Python模块学习------ 多线程