在程序退出之前做一些事情

发布于 2024-09-25 18:15:24 字数 78 浏览 1 评论 0原文

如何拥有一个在程序退出之前执行的函数或其他东西?我有一个脚本将在后台不断运行,我需要它在退出之前将一些数据保存到文件中。有这样做的标准方法吗?

How can you have a function or something that will be executed before your program quits? I have a script that will be constantly running in the background, and I need it to save some data to a file before it exits. Is there a standard way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

苯莒 2024-10-02 18:15:24

查看 atexit 模块:

http://docs.python.org/ library/atexit.html

例如,如果我想在应用程序终止时打印一条消息:

import atexit

def exit_handler():
    print 'My application is ending!'

atexit.register(exit_handler)

请注意,这对于脚本的正常终止非常有效,但在所有情况下都不会调用它(例如致命的内部错误)。

Check out the atexit module:

http://docs.python.org/library/atexit.html

For example, if I wanted to print a message when my application was terminating:

import atexit

def exit_handler():
    print 'My application is ending!'

atexit.register(exit_handler)

Just be aware that this works great for normal termination of the script, but it won't get called in all cases (e.g. fatal internal errors).

孤君无依 2024-10-02 18:15:24

如果您希望某些内容始终运行,即使出现错误,请像这样使用 try:finally: -

def main():
    try:
        execute_app()
    finally:
        handle_cleanup()

if __name__=='__main__':
    main()

如果您还想处理异常,您可以在 之前插入 except:代码>最后:

If you want something to always run, even on errors, use try: finally: like this -

def main():
    try:
        execute_app()
    finally:
        handle_cleanup()

if __name__=='__main__':
    main()

If you want to also handle exceptions you can insert an except: before the finally:

眼中杀气 2024-10-02 18:15:24

如果您通过引发KeyboardInterrupt(例如按Ctrl-C)来停止脚本,您可以将其作为标准异常捕获。您也可以用同样的方式捕获SystemExit

try:
    ...
except KeyboardInterrupt:
    # clean up
    raise

我提到这一点只是为了让您了解这一点;执行此操作的“正确”方法是上面提到的 atexit 模块。

If you stop the script by raising a KeyboardInterrupt (e.g. by pressing Ctrl-C), you can catch that just as a standard exception. You can also catch SystemExit in the same way.

try:
    ...
except KeyboardInterrupt:
    # clean up
    raise

I mention this just so that you know about it; the 'right' way to do this is the atexit module mentioned above.

深海夜未眠 2024-10-02 18:15:24

这是根据其他答案改编的版本。
它应该可以正常退出、终止和 PyCharm 停止按钮(我可以确认的最后一个)(尚未完全测试)。

import signal
import atexit


def handle_exit(*args):
    try:
        ... do computation ...
    except BaseException as exception:
        ... handle the exception ...


atexit.register(handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
signal.signal(signal.SIGINT, handle_exit)

This is a version adapted from other answers.
It should work (not fully tested) with graceful exits, kills, and PyCharm stop button (the last one I can confirm).

import signal
import atexit


def handle_exit(*args):
    try:
        ... do computation ...
    except BaseException as exception:
        ... handle the exception ...


atexit.register(handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
signal.signal(signal.SIGINT, handle_exit)
姜生凉生 2024-10-02 18:15:24

这可以处理正常退出以及使用 killCtrl+C 终止进程:

import sys
import atexit
import signal

def exit_handler():
    print("Cleaning up")

def kill_handler(*args):
    sys.exit(0)

atexit.register(exit_handler)
signal.signal(signal.SIGINT, kill_handler)
signal.signal(signal.SIGTERM, kill_handler)

# MAIN PROGRAM
# for example just reading from the input:
input("Press enter: ")

This can handle normal exit as well as killing the process with kill or Ctrl+C:

import sys
import atexit
import signal

def exit_handler():
    print("Cleaning up")

def kill_handler(*args):
    sys.exit(0)

atexit.register(exit_handler)
signal.signal(signal.SIGINT, kill_handler)
signal.signal(signal.SIGTERM, kill_handler)

# MAIN PROGRAM
# for example just reading from the input:
input("Press enter: ")

李不 2024-10-02 18:15:24

如果您有在程序的整个生命周期中都存在的类对象,您还可以使用 __del__(self) 方法从类中执行命令:

class x:
def __init__(self):
    while True:
        print ("running")
        sleep(1)

def __del__(self):
    print("destructuring")


a = x()

这也适用于正常程序结束,如果执行被中止,肯定会有一些例外:

running
running
running
running
running
Traceback (most recent call last):
  File "x.py", line 14, in <module>
    a = x()
  File "x.py", line 8, in __init__
    sleep(1)
KeyboardInterrupt
destructuring

If you have class objects, that exists during the whole lifetime of the program, you can also execute commands from the classes with the __del__(self) method:

class x:
def __init__(self):
    while True:
        print ("running")
        sleep(1)

def __del__(self):
    print("destructuring")


a = x()

this works on normal program end as well if the execution is aborted, for sure there will be some exceptions:

running
running
running
running
running
Traceback (most recent call last):
  File "x.py", line 14, in <module>
    a = x()
  File "x.py", line 8, in __init__
    sleep(1)
KeyboardInterrupt
destructuring
小情绪 2024-10-02 18:15:24

您还可以使用上下文管理器执行类似于 @Brian C. Lane 的回答的操作:

from contextlib import closing


class RunThat:
    def __init__(self) -> None:
        pass

    def execute_app(self) -> None:
        print("computation")

    def close(self) -> None:
        print("handle_cleanup")


with closing(RunThat()) as rt:
    rt.execute_app()

# --- which results in
# computation
# handle_cleanup

You could also use a context manager to do something similar to @Brian C. Lane's answer :

from contextlib import closing


class RunThat:
    def __init__(self) -> None:
        pass

    def execute_app(self) -> None:
        print("computation")

    def close(self) -> None:
        print("handle_cleanup")


with closing(RunThat()) as rt:
    rt.execute_app()

# --- which results in
# computation
# handle_cleanup
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文