Signals are software interrupts. Most nontrivial application programs need to deal with signals. Signals provide a way of handling asynchronous events—for example, a user at a terminal typing the interrupt key to stop a program or the next program in a pipeline terminating prematurely.
First, every signal has a name. These names all begin with the three characters SIG. For example, SIGABRT is the abort signal that is generated when a process calls the abort function. SIGALRM is the alarm signal that is generated when the timer set by the alarm function goes off. Signals are identified by integers and are defined in the operating system C headers. Python exposes the signals appropriate for the platform as symbols in the signal module.
We can tell the kernel to do one of three things when a signal occurs. We call this the disposition of the signal, or the action associated with a signal.
Ignore the signal.
Catch the signal. To do this, we tell the kernel to call a function of ours whenever the signal occurs. In our function, we can do whatever we want to handle the condition.
Let the default action apply. Every signal has a default action, note that the default action for most signals is to terminate the process.
Common signals we often see:
SIGINT: This signal is generated by the terminal driver when we press the interrupt key (often DELETE or Control - C). This signal is sent to all processes in the foreground process group (refer to Figure 9.9). This signal is often used to terminate a runaway program, especially when it’s generating a lot of unwanted output on the screen.
SIGERM: This is the termination signal sent by the kill(1) command by default. Because it can be caught by applications, using SIGTERM gives programs a chance to terminate gracefully by cleaning up before exiting (in contrast to SIGKILL, which can’t be caught or ignored).
python signal module
The module signal provides mechanisms to use signal handlers in Python. Some general rules for working with signals and their handlers.
The variables defined in the signal module are: signal.SIG_DFL, signal.SIG_IGN, SIG*, signal.signal(signalnum, handler) and so on.
signal.SIG_DFL: This is one of two standard signal handling options; it will simply perform the default function for the signal. For example, on most systems the default action for SIGQUIT is to dump core and exit, while the default action for SIGCHLD is to simply ignore it.
signal.SIG_IGN: This is another standard signal handler, which will simply ignore the given signal.
signal.signal(signalnum, handler): Set the handler for signal signalnum to the function handler. handler can be a callable Python object taking two arguments (see below), or one of the special values signal.SIG_IGN or signal.SIG_DFL. The previous signal handler will be returned (see the description of getsignal() above). The handler is called with two arguments: the signal number and the current stack frame (None or a frame object; for a description of frame objects).
Demo
As with other forms of event-based programming, signals are received by establishing a callback function, called a signal handler, that is invoked when the signal occurs. The arguments to your signal handler are the signal number and the stack frame from the point in your program that was interrupted by the signal.
import signal
import time
import sys
import os
def handle_int(sig, frame):
print "get signal: %s, I will quit"%sig
sys.exit(0)
def handle_hup(sig, frame):
print "get signal: %s"%sig
if __name__ == "__main__":
signal.signal(2, handle_int)
signal.signal(1, handle_hup)
print "My pid is %s"%os.getpid()
while True:
time.sleep(3)
This relatively simple example script loops indefinitely, pausing for a few seconds each time. When a signal comes in, the sleep call is interrupted and the signal handle_int for signal 2 and handle_hup for signal 1) prints the signal number(default handle for the other signal). Here we can use command kill to send signal in other terminal. We use kill -HUP pid or kill -1 pid to send signal 1, and ctrl-c in the same terminal or kill -2 pid to send signal 2.
$ python demo.py
My pid is 74608
get signal: 1
get signal: 1
^Cget signal: 2, I will quit
Signal
Signals are software interrupts. Most nontrivial application programs need to deal with signals. Signals provide a way of handling asynchronous events—for example, a user at a terminal typing the interrupt key to stop a program or the next program in a pipeline terminating prematurely.
First, every signal has a name. These names all begin with the three characters SIG. For example, SIGABRT is the abort signal that is generated when a process calls the abort function. SIGALRM is the alarm signal that is generated when the timer set by the alarm function goes off. Signals are identified by integers and are defined in the operating system C headers. Python exposes the signals appropriate for the platform as symbols in the
signal
module.We can tell the kernel to do one of three things when a signal occurs. We call this the disposition of the signal, or the action associated with a signal.
Common signals we often see:
python signal module
The module signal provides mechanisms to use signal handlers in Python. Some general rules for working with signals and their handlers.
The variables defined in the signal module are: signal.SIG_DFL, signal.SIG_IGN, SIG*, signal.signal(signalnum, handler) and so on.
signal.SIG_DFL
: This is one of two standard signal handling options; it will simply perform the default function for the signal. For example, on most systems the default action for SIGQUIT is to dump core and exit, while the default action for SIGCHLD is to simply ignore it.signal.SIG_IGN
: This is another standard signal handler, which will simply ignore the given signal.signal.signal(signalnum, handler)
: Set the handler for signal signalnum to the function handler. handler can be a callable Python object taking two arguments (see below), or one of the special values signal.SIG_IGN or signal.SIG_DFL. The previous signal handler will be returned (see the description of getsignal() above). The handler is called with two arguments: the signal number and the current stack frame (None or a frame object; for a description of frame objects).Demo
As with other forms of event-based programming, signals are received by establishing a callback function, called a signal handler, that is invoked when the signal occurs. The arguments to your signal handler are the signal number and the stack frame from the point in your program that was interrupted by the signal.
This relatively simple example script loops indefinitely, pausing for a few seconds each time. When a signal comes in, the sleep call is interrupted and the signal handle_int for signal 2 and handle_hup for signal 1) prints the signal number(default handle for the other signal). Here we can use command
kill
to send signal in other terminal. We usekill -HUP pid
orkill -1 pid
to send signal 1, andctrl-c
in the same terminal orkill -2 pid
to send signal 2.Ref
Python进程间异步通信之signal模块
signal – Receive notification of asynchronous system events