12345678910111213141516171819202122232425262728 |
- import atexit
- import threading
- import time
- # 定义一个退出时要执行的函数
- def exit_handler():
- print("Exiting the program")
- # 注册退出处理函数
- atexit.register(exit_handler)
- # 定义一个函数,在独立的线程中执行退出处理
- def exit_thread():
- time.sleep(1) # 模拟需要一些时间来执行退出处理
- atexit._run_exitfuncs() # 执行所有注册的退出处理函数
- # 创建一个独立线程来执行退出处理
- exit_thread = threading.Thread(target=exit_thread)
- exit_thread.daemon = True # 设置为守护线程,确保程序退出时会终止该线程
- exit_thread.start()
- # 模拟主线程的阻塞操作
- while True:
- pass
|