Technical background
For some Python programs that run continuously or for a long time, such as the backend of a server, or a scientific computing program that runs for a long time. When we are involved in some mid-exit operations, such as using Ctrl+C to exit a running program. There are generally two possibilities for this scenario: one is that there is something wrong with the program and it needs to be terminated to make adjustments to it. The other is that the program itself is correct, but the program is running too slowly, or it may want to end early. In this scenario, we often want to keep its corresponding calculation results. However, if we use some third-party data storage format to store the data, it may not be able to support continuous storage, and it is very common to save the result after the program execution is finished. However, since the program is terminated early, some special means are needed to save the results of the program that is terminated in the middle.
base case
Let's look at a simple case: a normal program that prints numbers every 1s, we can use python to capture this termination signal.
# signal_exit.py import signal import sys def signal_handler(signal, frame): print ('\nSignal Catched! You have just type Ctrl+C!') (0) if __name__ == '__main__': import time (, signal_handler) for x in range(100): (1) print (x)
When we run this program halfway through and press Ctrl+C at the same time, we get the following:
$ python3 signal_exit.py
0
1
2
^C
Signal Catched! You have just type Ctrl+C!
This result shows that we have captured the external operation of Ctrl+C in the process of running the program, and processed the operation accordingly before terminating the program. It should be noted that, if you do not add (0) the termination of the operation, the program will not be stopped, it will continue to run, which is equivalent to just capture the abnormal termination signal but do not do anything about it.
Pass external parameters to the termination signal
In the above case, we have only captured the external signal "terminate run", but if we want to capture the last output number, how do we do it? The function itself does not support a lot of parameter input, it is recommended to take is to create a class, signal_handler function encapsulated as a member function of the class, so that we can get the corresponding internal parameters, as shown in the following case:
# signal_exit.py import signal import sys import time class Printer: def __init__(self): = 0 (, self.signal_handler) def signal_handler(self, signal, frame): print ('\nSignal Catched! You have just type Ctrl+C! The last number is: {}'.format()) (0) def run(self, counter=10): while < counter: print () (1) += 1 if __name__ == '__main__': printer = Printer() (counter=100)
At this point, if you press Ctrl+C at the same time while the program is running, you get the following result:
$ python3 signal_exit.py
0
1
2
3
^C
Signal Catched! You have just type Ctrl+C! The last number is: 3
As you can see, we successfully captured the last parameter that was output.
Summary outline
When we are ready to kill a process, from the program design itself, we should design a certain protection scheme to ensure that when the program is terminated abnormally, the corresponding calculation results can also be well preserved. In Python, you can use functions to do this, but if you want to save data, you need to combine it with an actual class.
This article on Python3 to capture the Ctrl + C termination of the signal is introduced to this article, more related to Python capture Ctrl + C termination of the signal content, please search for my previous posts or continue to browse the following articles hope that you will support me in the future more!