Signal (signal) - The method of communication between processes is a software interrupt. Once a process receives a signal, it interrupts the original program execution flow to handle the signal.
Several common signals:
SIGINT Terminate process Interrupt process (control+c)
SIGQUIT Exit process
SIGTERM Terminate process Software termination signal (default Signal)
SIGKILL Terminate process Kill process
SIGALRM Alarm signal
For example, when pressing control+c to terminate the process, do Some processing, the code is as follows:
import signal
def fun(sig, stack_frame):
print 'eixt %d, %s' % (sig, stack_frame)
Exit(1)
signal.signal(signal.SIGINT, fun)
signal.signal(signal.SIGINT, fun) is used to register Semaphore processingFunction,
The first parameter is the semaphore, and the second parameter can be passed to a function.
This function has two parameters by default, The first parameter sig represents the received semaphore, and the second stack_frame can be understood as the call stack that generates the interrupt semaphore sig.
The above is the detailed content of Detailed explanation of Python signals. For more information, please follow other related articles on the PHP Chinese website!