- 主線程等待子線程執行完再結束
# 導包
from threading import *
from time import *
def dance():
print(current_thread())
for i in range(6):
print('跳舞...')
sleep(0.2)
if __name__ == '__main__':
dance_thread = Thread(target=dance)
dance_thread.start()
sleep(0.5)
print('over')
- 主線程結束時直接銷毀子線程
# 導包
from threading import *
from time import *
def dance():
print(current_thread())
for i in range(6):
print('跳舞...')
sleep(0.2)
if __name__ == '__main__':
# 將該線程設置為守護主線程
dance_thread = Thread(target=dance)
dance_thread.setDaemon(True)
dance_thread.start()
sleep(0.5)
print('over')