Python Learning Note 14: Standard library semaphore (signal package)

Source: Internet
Author: User
Tags signal handler

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), and the Windows kernel is not fully supported by the signaling mechanism.
So python on Windows does not function as a signaling system.

Define the signal name the signal package defines the individual signal names and their corresponding integers, such as
Import Signalprint signal. Sigalrmprint signal. Sigcont

Python uses the same signal name as Linux. can be done by
$man 7 Signal
Inquire


The core of the default signal processing function signal package is to use the signal.signal () function to preset (register) signal processing functions, 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 recerive signal:", Signum) # Regis ter signal. SIGTSTP ' s handlersignal.signal (signal. SIGTSTP, MyHandler) siganl.pause () print ("End")

In the main program, 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 you 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 send a signal.
Process does not have to use Signal.pause () to pause to wait for a signal, it can also accept signals in the work,
For example, change the above Signal.pause () to a cycle that takes a long time to work.
You can change the actions in MyHandler () to suit your needs to personalize the processing for different signals.

Import signal# Define Signal handler functiondef MyHandler (Signum, frame):    print ("Now,it ' s Time") # Register signal. SIGTSTP ' s handlersignal.signal (signal. SIGALRM, MyHandler) Signal,alarm (5) while True:    print ("End")

An infinite loop is used here to keep the process running.
After Signal.alarm () executes for 5 seconds, the process sends itself a SIGALRM signal, and then the signal handler function MyHandler begins execution.


The core of the send signal signal packet is to set the signal processing function.
In addition to the Signal.alarm () sends a signal to itself, there is no other function to send the signal.
In the OS package, however, there are functions similar to the Linux Kill command, respectively
Os.kill (PID, sid)
OS.KILLPG (Pgid, sid)
Send signals to process and process groups (see Linux process Relationships), respectively. The SID is the integer or singal that corresponds to the signal. sig*.
In fact, signal, pause,kill and alarm are all common C library functions in Linux application programming, and here we are only implementing them in Python language.
In fact, Python's interpreter is written in C, so this similarity is not surprising.
In addition, in Python 3.4, the signal package is enhanced, and the signal blocking feature is added to the package.

Python Learning Note 14: Standard library semaphore (signal package)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.