The signal module in the Python standard library is further studied based on the learning basis of the Python learning note-the--linux signal basis for multi-process communication.
Although signal is a module in Python, it is primarily for UNIX platforms (such as the Linux,mac OS), and the Windows kernel has insufficient support for signaling mechanisms, so Python on Windows does not function as a signaling system.
The signal module is responsible for signal processing within the Python program, and typical operations include signal processing functions, pausing and waiting for signals, and timing out SIGALRM, etc.
1 Signal Basic Signal name
Introduction module: import signal
Signal. SIGHUP # connection hangs off; Signal. Sigill # illegal instruction; Signal. SIGINT # Terminate process (CTRL + C); Signal. SIGTSTP # pause process (ctrl + Z); Signal. SIGKILL # kill process (this signal cannot be captured or ignored); Signal. Sigquit # terminal exit; Signal. SIGTERM # termination signal, software termination signal; Signal. SIGALRM # Alarm clock signal, initiated by Signal.alarm (); Signal. Sigcont # continue execution of the pause process;
2 common signal processing functions 2.1
设置发送SIGALRM信号的定时器
Signal.alarm (Time)
Parameter: Time parameter
Function: Sends a SIGALRM signal to the process itself after time
Import Signal Import Timesignal.alarm (4) # terminate program after 4s while True: time.sleep (1) print(" learn python ... ")
Run
Learn python in ... Learn python in ... Learn python in ... Alarm clock
When two signal.alarm () functions appear in a program
Import Signal Import Time Print # 0time.sleep (1)print# 2 while True: time.sleep (1) print(" learn python ... ")
Run
02 learning python ... Learn python in ... Learn python in ... Alarm Clock
Note: In a process, only one clock can be set, if the second is set to overwrite the time of the first, return to the time remaining, the first alarm returns 0.
2.1 Setting Signal processing functions
Signal.signal (SIG, Handler)
function : processing function of signal processing scheme according to Handler
Parameters :
SIG: Signal to be processed, processing signal only for this kind of signal function sig
hander: Signal processing scheme
As mentioned in the Signal Foundation, the process can ignore the signal, can take the default action, and can also customize the operation, when handler is the following function, it will have the following actions:
Sig_ign: Signal is ignored (ignore) or ignored
SIG_DFL: Process is handled by default behavior
When Function:handler is a function name, the process uses a custom function to handle
*sigstop Sigkill can not be processed, can only be used
Python Learning note-Signal module signal