Pycharm:强制停止其他代码后自动运行 .py 代码

问题描述 投票:0回答:1

我正在使用 Pycharm 并且有一个 24/7 运行的代码。有时我需要强制停止这个主要代码。当我强制停止主程序时,有没有办法自动启动另一个“清理”py 文件?

python automation pycharm
1个回答
0
投票
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()

© www.soinside.com 2019 - 2024. All rights reserved.