With the understanding of the signal base of Linux, the signal package in the Python standard library is easy to learn and understand. The signal package is responsible for processing signals inside the Python program, typically including pre-programmed signal processing functions, pausing and waiting for signals, and timing out SIGALRM. Note that the signal package is primarily for UNIX platforms (such as Linux, MAC OS), while the Windows kernel is not fully supported by signaling mechanisms, so Python on Windows does not function as a signaling system.
Signal (signal)--the way communication between processes is a software interrupt. Once a process receives a signal, it interrupts the original program execution process to process the signal.
Define the signal name
The signal package defines the individual signal names and their corresponding integers, such as:
Python uses the same signal name as Linux and can be queried by $ man 7 signal
Preset Signal processing functions
The core of the signal package is to use the signal.signal () function to preset the (register) signal processing function as follows:
Singnal.signal (Signalnum, handler)
Signalnum is a signal, handler is the processing function of the signal. We mentioned in the signal base that the process can ignore the signal, can take the default action, and can customize the operation. When handler is signal. Sig_ign, the signal is ignored (ignore). When handler is Singal. SIG_DFL, the process takes the default action. When handler is a function name, the process takes the action defined in the function.
Import Signal
# Define Signal Handler functiondef MyHandler (Signum, frame): print (' I received: ', Signum) # Register signal. SIGTSTP ' s handlersignal.signal (signal. SIGTSTP, MyHandler) signal.pause () print (' End of Signal Demo ') # problems to be tested
In the main program, we first use the signal.signal () function to preset the signal processing function. We then execute Signal.pause () to let the process pause to wait for the signal. When the signal SIGUSR1 is passed to the process, the process resumes from the pause and, according to the preset, executes the SIGTSTP signal handler function MyHandler (). MyHandler two parameters one is used to identify the signal (Signum), and the other is used to obtain the status of the process stack when the signal occurs (stack frame). Both of these parameters are passed by the signal.singnal () function.
The above program can be saved in a file (such as test.py). We use the following method to run:
$python test.py
For the process to run. When the program runs to Signal.pause (), the process pauses and waits for a signal. At this point, a SIGTSTP signal is sent to the process by pressing CTRL + Z. As we can see, the process executes the myhandle () function, which is then returned to the main program to continue execution. (Of course, you can also query the process ID with $ps, and then use $kill to signal.) )
(The process does not have to use Signal.pause () to pause to wait for a signal, it can also accept signals in the work, such as changing the above signal.pause () to a cycle that takes a long time to work. )
We can change the operations in MyHandler () to suit our needs to personalize the processing for different signals.
Timing out SIGALRM Signals
A useful function is Signal.alarm (), which is used to send a SIGALRM signal to the process itself after a certain amount of time:
Import signal# Define Signal handler functiondef MyHandler (Signum, frame): print ("Now, it's the time") exit () # RE Gister signal. Sigalrm ' s handler signal.signal (signal. SIGALRM, MyHandler) Signal.alarm (5) while True: print (' not yet ')
Signal of the Python module (signal)