Python Linux signal programming signals, pythonsignals
1. Signal Introduction
The Soft Interrupt signal (signal) is used to notify the process of an asynchronous event. Processes can call kill to send Soft Interrupt signals to each other. The kernel can also send a signal to the process due to internal events to notify the process of an event. Note that the signal is only used to notify a process of events and does not transmit any data to the process. Signal is a part of process control and a very important part in Linux programming.
2. programming example
The main steps for python programming are:
1) import the signal module.
2) Compile the signal processing function.
3) bind signals and signal processing functions.
4) processing signals
Use SIGINT as an example to compile sigtest. py. Then use "CTRL + C" to interrupt
#! /Usr/bin/env python #-*-coding: UTF-8-*-import signaldef ctrlc_handler (sig_num, frm): print "La la, you can't end my handler _^ "print" Init signal handler... "signal. signal (signal. SIGINT, ctrlc_handler) print "Done! "While True: pass
The result shows that our custom signal processing function is running.
Someone asked, What should I do if the program cannot end? Don't worry, usePs
Command to list the process list, find its PID, and Kill it.