在一个死循环中,不使用return、break的情况下,强制退出程序。
#python #结束进程 #多线程
Python入门题039:退出程序(5种方法)
代码1:
import os
import sys
import time
def stop():
sys.exit()
# quit()
# exit()
# raise SystemExit
# os._exit(0)
while True:
print('hello')
stop()
time.sleep(.5)
代码2:
import os
import sys
import time
def stop():
# sys.exit()
# quit()
# exit()
# raise SystemExit
os._exit(0)
while True:
print('hello')
try:
stop()
except SystemExit as e:
print('接收到退出命令:', type(e))
time.sleep(.5)
代码3:
import os
import sys
import time
import threading
def stop():
# sys.exit() # 退出线程
# exit() # 退出线程
# raise SystemExit # 退出线程
# quit() # 退出线程
os._exit(0) # 退出进程
while True:
print('hello')
threading.Thread(target=stop).start()
# stop()
time.sleep(.5)
页面更新:2024-03-30
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号