Day Vision

Reading Everyday,Extending Vision

How to catch program exit exceptions and user-enforced exits in Python

2023-02-28 03:58:39


Let's say we write a Python program with no interface, which contains the code to call a hardware device, such as: Tsmaster TC1016, the general hardware call process is similar; Call before the need to do interface initialization, and then call the function, call a good program before we exit


First, why do we need to catch Python program aborts and user exits?

Let's first illustrate through a scenario, suppose we write a Python program without an interface, which contains code to call a hardware device, such as: Tsmaster's TC1016, the general hardware call process is similar; Before calling, we need to initialize the interface, then call each function, and before the call program exits, we need to release hardware resources to prepare for the next call.

So the question is, what happens if the program does not do to release hardware resources before exiting? The answer is uncertain, some hardware drivers with more complete functions, including the function of self-reset, then it is possible to exit before not releasing hardware resources is OK, but some drivers are not, once the resources are not released, the next time can not be initialized, only unplug the USB or restart the computer can be solved.

Then you will ask again, under normal circumstances, I in the program before exiting, add a step operation to release hardware resources can not solve this problem?


Turn off. PNG

This article mainly focuses on these two special situations.

First of all, when the program exits automatically when an exception occurs, we can use Python's built-in ATEXIT function to capture,

下面是一个简单的实例:

 atexit  

To execute this program, when the program runs to sleep (777) and waits for the code, we either press the stop button in pycharm or manually press Crtl C in the terminal to simulate a KeyboardInterrupt exception that terminates the program.

So in the future, we can use atexit to catch the automatic exit when the program itself is abnormal.Next, back to the previous topic, if the user closes the program directly through the X button in the terminal interface, can atexit also capture it?

We've shown experimentally that Atexit can't do anything about the X button closing method, so is there a way to capture this? The answer is yes.

Let's use an example to demonstrate it.

 win32api

We actually verify the effect, through Pyinstaller package the above program as an EXE file, and then run it, and in the process of running the X button to close the program, before the program closed, we hear a beep sound, which indicates that the callback function registered by Setconsolectrlhandler is in effect.

总结:

  1. In practice, we can imitate the example and replace the beep part of the code for the code released by the interface.We must do a good job of releasing the hardware after each hardware call due to abnormal exit or forced shutdown to prevent the call failure or memory leakage.
  2. 第二种SetConsoleCtrlHandler的方法虽然比atexit适用范围更广且更强大,但其需要安装第三方库支持pywin32(pip install pywin32)。