我正在使用 Pycharm 并且有一个 24/7 运行的代码。有时我需要强制停止这个主要代码。当我强制停止主程序时,有没有办法自动启动另一个“清理”py 文件?
import sys
"""
Try the following code.
If running in PyCharm, press the stop button to cause clean_up to run.
If running the program, press CTRL+C to cause clean_up to run.
"""
def func1():
x = 1
return x
def func2():
y = 2
return y
def clean_up():
print('Cleanup complete')
sys.exit(0)
while True:
# Run all the functions in my main code
try:
func1()
func2()
except KeyboardInterrupt:
# Clean up if main code is forced to stop.
clean_up()