程序運行時遭遇未知錯誤,不知如何解決,總之重啟就好使,那麼就設置錯誤時或者將要錯誤時自動重啟吧
import os
import sys
import threading
import time
t=None
def kill_thread(t: threading.Thread):
import ctypes
if t is not None:
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(t.ident), ctypes.py_object(SystemExit))
def restart_program():
print("重啟。。。。。。。")
python = sys.executable
os.execl(python, python, *sys.argv)
def func():
while True:
time.sleep(1)
task={'task':''}
num=0
while True:
print(f'main: {len(threading.enumerate())}')
if num == 5:
t = threading.Thread(target=func)
t.start()
task['task']=t
elif num==10:
kill_thread(task['task'])
t=None
restart_program()
num += 1
time.sleep(1)
print(num)