建立函式並且傳入Thread物件中t.py指令碼內容importthreading,timefromtimeimportsleep,ctimedefnow(): returnstr(time.strftime("%Y-%m-%d%H:%M:%S",time.localtime()))deftest(nloop,nsec): print"startloop",nloop,"at:",now() sleep(nsec) print"loop",nloop,"doneat:",now()defmain(): print"startingat:",now() threadpool=[] foriinxrange(10): th=threading.Thread(target=test,args=(i,2)) threadpool.append(th) forthinthreadpool: th.start() forthinthreadpool: threading.Thread.join(th) print"allDoneat:",now()if__name__=="__main__": main()
建立函式並且傳入Thread物件中t.py指令碼內容importthreading,timefromtimeimportsleep,ctimedefnow(): returnstr(time.strftime("%Y-%m-%d%H:%M:%S",time.localtime()))deftest(nloop,nsec): print"startloop",nloop,"at:",now() sleep(nsec) print"loop",nloop,"doneat:",now()defmain(): print"startingat:",now() threadpool=[] foriinxrange(10): th=threading.Thread(target=test,args=(i,2)) threadpool.append(th) forthinthreadpool: th.start() forthinthreadpool: threading.Thread.join(th) print"allDoneat:",now()if__name__=="__main__": main()