In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.