Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to force a piece of code to run when Python exits

Shulou Source: shulou.com Published: 2022-06-01 18:37:25 09月29日 Update

This article will explain in detail how to force a piece of code to run when Python exits. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Imagine a scenario where you want to develop a test program for a project. When the program starts running, the initial environment is created, and after the test is completed, the environment is cleaned up.

Imagine a scenario where you want to develop a test program for a project. When the program starts running, the initial environment is created, and after the test is completed, the environment is cleaned up.

The logic itself is very simple:

Setup () test () clean ()

However, due to the complexity of the tested code, you always have an exception when debugging, resulting in a crash every time the clean () function is not run.

You might wonder what would happen if you wrote like this:

Setup () try: text () except Exception as e: print ('run exception:', e) clean ()

It seems that the program must run to the clean () function, but if you write a lot of code, you should know that try...except... is abused. It will cause you a lot of pain. For example, it suddenly prints you a run exception: 1. You have no idea what went wrong, or exactly what went wrong. In order to find the problem, you must let the program expose the error. But as a result, clean () doesn't work properly again.

Is there any way to not only make the program report an error, but also run clean () when the error is reported?

At this point, we can use the atexit module that comes with Python. How to use it is very simple:

Import atexit @ atexit.register def clean (): print ('clean up the environment-related code') setup () test ()

In this way, we do not need to explicitly call the clean function. Regardless of the normal end of the program or the abnormal error of the program, the contents of the clean function will always be executed.

As shown in the following figure:

There are the following considerations in using atexit:

You can register multiple exit functions, and they will execute them according to the registration time from late to early morning. For example:

Import atexit @ atexit.register def clean_1 ():... @ atexit.register def clean_2 ():... Will run clean_2 () and then clean_1 ()

If the clean () function has arguments, you can call atexit.register (clean_1, argument 1, argument 2, argument 3, arguments xxxx') without a decorator.

If the program is killed by a system signal that you have not processed, the registered function will not execute properly.

If a serious Python internal error occurs, the function you registered will not execute properly.

If you call os._exit () manually, the function you registered will not execute properly.

This is how to force a piece of code to run when Python exits. I hope the above can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Tags: Run program function code environment test parameter time content problem force in this way will create scene article more knowledge article error Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Huawei Linux Xiaomi