論多執行緒python2與python3
阿新 • • 發佈:2018-11-08
python3 常用執行緒
# -*- coding: utf-8 -*- import time from threading import Thread def test(i): while True: print("i",i+1) time.sleep(1) i+=1 if i == 5: break print(1) t = Thread(target=test, args=(0,))
#t.setDaemon(True)
t.start()
print("---",2)
主執行緒一直執行,遇到迴圈耗時操作分出子執行緒,主執行緒執行到最後等待子執行緒結束,再進行關閉
python2.7 thread方法
# -*- coding: utf-8 -*- import time # from threading import Thread import thread def test(i): while True: print "i",i+1 time.sleep(1) i+=1 if i == 5: break print 1 thread.start_new_thread(test, (0,)) # t = Thread(target=test, args=(0,))# t.start() print "---",2
主執行緒執行到最後就結束,相當於python3中設定了守護進行,如上註釋部分所示