import threading
import time
def background_task():
print("執行緒已啟動,等待5秒後結束程式...")
time.sleep(5) # 等待5秒
print("程式結束")
exit()
thread = threading.Thread(target=background_task)
thread.daemon = True # 設置執行緒為守護執行緒,使程式退出時可以自動結束執行緒
thread.start()
while True:
pass