Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to force a section of code to run when Python exits". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to force a section of code to run when Python exits".

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.

Thank you for your reading, the above is the content of "how to force a section of code to run when Python exits". After the study of this article, I believe you have a deeper understanding of how to achieve a mandatory run of code when Python exits, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report