多線程Threading
多線程之間的資源是共享的,多線程之間是併發的
多線程的實現
import threading
import time
def music(name):
print('%s begin to listen to music %s'%(name,time.ctime()))
time.sleep(3)
print('%s end to listen to music %s'%(name,time.ctime()))
def game(name):
print('%s begin to play game %s'%(name,time.ctime()))
time.sleep(5)
print('%s end to play game %s'%(name,time.ctime()))
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))#實例化多線程
t2=threading.Thread(target=game,args=('alex',))
t1.start()#開始執行多線程
t2.start()
print('end....') #注意時間只要5秒就完成
'''alex begin to listen to music Tue Apr 17 21:32:50 2018
alex begin to play game Tue Apr 17 21:32:50 2018
end....
alex end to listen to music Tue Apr 17 21:32:53 2018
alex end to play game Tue Apr 17 21:32:55 2018'''
join
import threading
import time
def music(name):
print('%s begin to listen to music %s'%(name,time.ctime()))
time.sleep(3)
print('%s end to listen to music %s'%(name,time.ctime()))
def game(name):
print('%s begin to play game %s'%(name,time.ctime()))
time.sleep(5)
print('%s end to play game %s'%(name,time.ctime()))
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))#實例化多線程
t2=threading.Thread(target=game,args=('alex',))
t1.start()#開始執行多線程
t2.start()
t1.join() #join方法是讓主線程等待子線程t1結束後才繼續執行主線程
t2.join()
print('end....')
'''alex begin to listen to music Tue Apr 17 21:39:23 2018
alex begin to play game Tue Apr 17 21:39:23 2018
alex end to listen to music Tue Apr 17 21:39:26 2018
alex end to play game Tue Apr 17 21:39:28 2018
end....'''
若改成
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))#實例化多線程
t2=threading.Thread(target=game,args=('alex',))
t1.start()#開始執行多線程
t2.start()
t1.join() #join方法是讓主線程等待子線程t1結束後才繼續執行主線程
#t2.join()
print('end....')
'''alex begin to listen to music Tue Apr 17 21:41:11 2018
alex begin to play game Tue Apr 17 21:41:11 2018
alex end to listen to music Tue Apr 17 21:41:14 2018
end....
alex end to play game Tue Apr 17 21:41:16 2018'''
#t1在執行完畢後會立即執行print,然後等待執行t2
Setdaemon
import threading
import time
def music(name):
print('%s begin to listen to music %s'%(name,time.ctime()))
time.sleep(3)
print('%s end to listen to music %s'%(name,time.ctime()))
def game(name):
print('%s begin to play game %s'%(name,time.ctime()))
time.sleep(5)
print('%s end to play game %s'%(name,time.ctime()))
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))
t2=threading.Thread(target=game,args=('alex',))
t2.setDaemon(True) #必須在start前設置,一旦主線程結束,對應的子線程也結束
t1.start()
t2.start()
print('end....')
'''
alex begin to listen to music Thu Apr 19 15:22:42 2018
alex begin to play game Thu Apr 19 15:22:42 2018
end....
alex end to listen to music Thu Apr 19 15:22:45 2018
'''
#在打印end...後還要等待t1的運行,當t1運行完後,主線程纔算結束,t2也跟着結束
若改成
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))
t2=threading.Thread(target=game,args=('alex',))
t1.setDaemon(True) #必須在start前設置,一旦主線程結束,對應的子線程也結束
t1.start()
t2.start()
print('end....')
'''
alex begin to listen to music Thu Apr 19 15:26:46 2018
alex begin to play game Thu Apr 19 15:26:46 2018
end....
alex end to listen to music Thu Apr 19 15:26:49 2018
alex end to play game Thu Apr 19 15:26:51 2018
'''
#在打印end...之後要等待t2運行,但t1在t2運行結束之前已經運行,故當t2結束運行,主線程結束時t1也運行完畢
線程相關的其他方法
#Thread實例對象的方法
# isAlive(): 返回線程是否活動的。
# getName(): 返回線程名。
# setName(): 設置線程名。
#threading模塊提供的一些方法:
# threading.currentThread(): 返回當前的線程變量。
# threading.enumerate(): 返回一個包含正在運行的線程的list。正在運行指線程啓動後、結束前,不包括啓動前和終止後的線程。
# threading.activeCount(): 返回正在運行的線程數量,與len(threading.enumerate())有相同的結果。
from threading import Thread
import threading
def work():
import time
time.sleep(3)
print(threading.current_thread().getName())
if __name__ == '__main__':
#在主進程下開啓線程
t=Thread(target=work)
t.start()
print(threading.current_thread().getName())
print(threading.current_thread()) #主線程
print(threading.enumerate()) #連同主線程在內有兩個運行的線程
print(threading.active_count())
print('主線程/主進程')
'''
打印結果:
MainThread
<_MainThread(MainThread, started 140735268892672)>
[<_MainThread(MainThread, started 140735268892672)>, <Thread(Thread-1, started 123145307557888)>]
主線程/主進程
Thread-1
'''
主線程等待子線程結束
from threading import Thread
import time
def sayhi(name):
time.sleep(2)
print('%s say hello' %name)
if __name__ == '__main__':
t=Thread(target=sayhi,args=('alex',))
t.start()
t.join()
print('主線程')
print(t.is_alive())
'''
alex say hello
主線程
False
'''