如何终止脚本?
- 2024-12-06 08:40:00
- admin 原创
- 122
问题描述:
如何提前退出脚本,就像die()
PHP 中的命令一样?
解决方案 1:
import sys
sys.exit()
sys
模块文档中的详细信息:
sys.exit([arg])
退出 Python。这是通过引发
SystemExit
异常来实现的,因此将遵守语句的 finally 子句指定的清理操作try
,并且可以在外层拦截退出尝试。可选参数arg可以是一个给出退出状态的整数(默认为零),也可以是另一种类型的对象。如果它是一个整数,零被认为是“成功终止”,任何非零值都被 shell 等视为“异常终止”。大多数系统要求它处于 0-127 范围内,否则会产生未定义的结果。一些系统有为特定退出代码分配特定含义的约定,但这些约定通常还不够完善;Unix 程序通常使用 2 表示命令行语法错误,1 表示所有其他类型的错误。如果传递了另一种类型的对象,则 None 相当于传递零,并且任何其他对象都会被打印
stderr
并导致退出代码为 1。特别是,sys.exit("some error message")
这是一种在发生错误时退出程序的快速方法。由于
exit()
最终“仅”会引发异常,因此只有从主线程调用时才会退出进程,并且不会拦截异常。
请注意,这是退出的“好方法”。@ glyphtwistedmatrix指出,如果您想要“硬退出”,则可以使用os._exit(*errorcode*)
,尽管它在某种程度上可能特定于操作系统(例如,在 Windows 下可能不接受错误代码),并且它肯定不太友好,因为它不允许解释器在进程终止之前进行任何清理。另一方面,它确实会终止整个进程,包括所有正在运行的线程,而sys.exit()
(如文档中所述)只有在从主线程调用时才会退出,没有其他线程在运行。
解决方案 2:
提前终止 Python 脚本的一个简单方法是使用内置quit()
函数。无需导入任何库,高效而简单。
例子:
#do stuff
if this == that:
quit()
解决方案 3:
另一种方法是:
raise SystemExit
解决方案 4:
您也可以简单地使用exit()
。
请记住sys.exit()
,exit()
、quit()
、 和os._exit(0)
会终止Python 解释器。因此,如果它出现在通过 调用另一个脚本的脚本中execfile()
,则会停止这两个脚本的执行。
请参阅“停止执行使用 execfile 调用的脚本”以避免这种情况。
解决方案 5:
虽然您通常应该更喜欢它sys.exit
,因为它对其他代码更“友好”,但它实际上所做的只是引发异常。
如果您确定需要立即退出某个进程,并且您可能处于某个会捕获的异常处理程序内部SystemExit
,那么还有另一个函数 - os._exit
- 它会立即在 C 级别终止并且不会执行任何正常的解释器拆卸操作;例如,不会执行使用“atexit”模块注册的钩子。
解决方案 6:
我刚刚发现,在编写多线程应用程序时,raise SystemExit
两者sys.exit()
都只终止正在运行的线程。另一方面,os._exit()
退出整个进程。这在“为什么在 Python 线程内调用 sys.exit() 时不会退出? ”中进行了讨论。
下面的例子有 2 个线程。Kenny 和 Cartman。Cartman 应该永远活着,但 Kenny 被递归调用,应该在 3 秒后死亡。(递归调用不是最好的方法,但我有其他原因)
如果我们想让卡特曼在肯尼死后也死,那么肯尼就应该离开os._exit
,否则,只有肯尼会死,而卡特曼会永远活着。
import threading
import time
import sys
import os
def kenny(num=0):
if num > 3:
# print("Kenny dies now...")
# raise SystemExit #Kenny will die, but Cartman will live forever
# sys.exit(1) #Same as above
print("Kenny dies and also kills Cartman!")
os._exit(1)
while True:
print("Kenny lives: {0}".format(num))
time.sleep(1)
num += 1
kenny(num)
def cartman():
i = 0
while True:
print("Cartman lives: {0}".format(i))
i += 1
time.sleep(1)
if __name__ == '__main__':
daemon_kenny = threading.Thread(name='kenny', target=kenny)
daemon_cartman = threading.Thread(name='cartman', target=cartman)
daemon_kenny.setDaemon(True)
daemon_cartman.setDaemon(True)
daemon_kenny.start()
daemon_cartman.start()
daemon_kenny.join()
daemon_cartman.join()
解决方案 7:
from sys import exit
exit()
您可以传递一个退出代码作为参数,该代码将返回给操作系统。默认值为 0。
解决方案 8:
我完全是新手,但这肯定更干净,更可控
def main():
try:
Answer = 1/0
print Answer
except:
print 'Program terminated'
return
print 'You wont see this'
if __name__ == '__main__':
main()
...
项目终止
比
import sys
def main():
try:
Answer = 1/0
print Answer
except:
print 'Program terminated'
sys.exit()
print 'You wont see this'
if __name__ == '__main__':
main()
...
程序终止回溯(最近一次调用):文件“Z:\Directory\testdieprogram.py”,第 12 行,在 main() 中文件“Z:\Directory\testdieprogram.py”,第 8 行,在 main sys.exit() 中 SystemExit
编辑
重点是节目能够顺利、平静地结束,而不是“我已经停下来了!!!!”
解决方案 9:
问题
在我的实践中,甚至出现过需要从其中一个进程中终止整个多处理器应用程序的情况。
如果您的应用程序只使用主进程,则以下函数可以很好地工作。但是在我的例子中,由于应用程序有许多其他活动进程,因此以下函数都有效。
quit()
exit(0)
os._exit(0)
sys.exit(0)
os.kill(os.getppid(), 9)
-父进程的pidos.getppid()
在哪里
最后一个终止了主进程和它自身,但是其余进程仍然存在。
解决方案
我不得不通过外部命令来杀死它,最后找到了使用的解决方案pkill
。
import os
# This can be called even in process worker and will kill
# whole application included correlated processes as well
os.system(f"pkill -f {os.path.basename(__file__)}")
解决方案 10:
在 Python 3.5 中,我尝试整合类似的代码,不使用内置模块(例如 sys、Biopy)来停止脚本并向用户打印错误消息。这是我的示例:
## My example:
if "ATG" in my_DNA:
## <Do something & proceed...>
else:
print("Start codon is missing! Check your DNA sequence!")
exit() ## as most folks said above
后来我发现直接抛出错误更加简洁:
## My example revised:
if "ATG" in my_DNA:
## <Do something & proceed...>
else:
raise ValueError("Start codon is missing! Check your DNA sequence!")
解决方案 11:
只需将其放在代码末尾quit()
即可关闭 Python 脚本。
解决方案 12:
在 Python 3.9 中,您还可以使用:raise SystemExit("Because I said so")
。
解决方案 13:
我的看法。
Python 3.8.1,Windows 10,64 位。
sys.exit()
对我来说不直接起作用。
我有几个下一个循环。
首先,我声明一个布尔变量,我称之为immediateExit
。
因此,在程序代码的开头我写道:
immediateExit = False
然后,从最内层(嵌套)循环异常开始,我写道:
immediateExit = True
sys.exit('CSV file corrupted 0.')
然后我进入外循环的立即继续,在代码执行任何其他操作之前,我写下:
if immediateExit:
sys.exit('CSV file corrupted 1.')
根据复杂程度,有时上述陈述也需要在除章节等之外的部分重复。
if immediateExit:
sys.exit('CSV file corrupted 1.5.')
自定义消息也用于我个人的调试,因为数字的用途相同 - 查看脚本真正退出的位置。
'CSV file corrupted 1.5.'
在我的特定情况下,我正在处理一个 CSV 文件,如果软件检测到它已损坏,我不希望软件接触它。因此对我来说,在检测到可能的损坏后立即退出整个 Python 脚本非常重要。
并且通过从所有循环中逐步退出 sys.exit,我成功地做到了这一点。
完整代码:(由于它是内部任务的专有代码,因此需要进行一些更改):
immediateExit = False
start_date = '1994.01.01'
end_date = '1994.01.04'
resumedDate = end_date
end_date_in_working_days = False
while not end_date_in_working_days:
try:
end_day_position = working_days.index(end_date)
end_date_in_working_days = True
except ValueError: # try statement from end_date in workdays check
print(current_date_and_time())
end_date = input('>> {} is not in the list of working days. Change the date (YYYY.MM.DD): '.format(end_date))
print('New end date: ', end_date, '
')
continue
csv_filename = 'test.csv'
csv_headers = 'date,rate,brand
' # not real headers, this is just for example
try:
with open(csv_filename, 'r') as file:
print('***
Old file {} found. Resuming the file by re-processing the last date lines.
They shall be deleted and re-processed.
***
'.format(csv_filename))
last_line = file.readlines()[-1]
start_date = last_line.split(',')[0] # assigning the start date to be the last like date.
resumedDate = start_date
if last_line == csv_headers:
pass
elif start_date not in working_days:
print('***
{} file might be corrupted. Erase or edit the file to continue.
***'.format(csv_filename))
immediateExit = True
sys.exit('CSV file corrupted 0.')
else:
start_date = last_line.split(',')[0] # assigning the start date to be the last like date.
print('
Last date:', start_date)
file.seek(0) # setting the cursor at the beginnning of the file
lines = file.readlines() # reading the file contents into a list
count = 0 # nr. of lines with last date
for line in lines: #cycling through the lines of the file
if line.split(',')[0] == start_date: # cycle for counting the lines with last date in it.
count = count + 1
if immediateExit:
sys.exit('CSV file corrupted 1.')
for iter in range(count): # removing the lines with last date
lines.pop()
print('
{} lines removed from date: {} in {} file'.format(count, start_date, csv_filename))
if immediateExit:
sys.exit('CSV file corrupted 1.2.')
with open(csv_filename, 'w') as file:
print('
File', csv_filename, 'open for writing')
file.writelines(lines)
print('
Removing', count, 'lines from', csv_filename)
fileExists = True
except:
if immediateExit:
sys.exit('CSV file corrupted 1.5.')
with open(csv_filename, 'w') as file:
file.write(csv_headers)
fileExists = False
if immediateExit:
sys.exit('CSV file corrupted 2.')
解决方案 14:
exit
在quit
.py 文件和sys.exit
exe 文件中使用