Python's signal signal

Source: Internet
Author: User

Signal signal is a mechanism for sending signals between Python processes, and the principle is that the operating system controls the process and is a program interrupt

Once a process receives a signal, it interrupts the original program execution process to process the signal.

So what's the use of singanl?

Application of Siganl :

1. Fault location technology ( The underlying failure of the process, such as a sudden process outage and some less likely failures )

2. Process Control of the process

First, several functions related to signal signal

(1) Os.kill (PID,SIG)

Send a signal to a process

Parameter resolution:

PID Specifies the process number to send the signal

SIG signal designator to be sent (need to be obtained via signal module)

(2) Signal.alarm (SEC) Non-blocking function

Set the clock signal to send itself a SIGALRM signal after a certain amount of time

Principle: Clock creation is a process that is created by the operating system kernel (kernal)

The clock and process are executed asynchronously, and when the clock comes, the kernel sends a signal to the process, and the process receives the signal to respond accordingly. This is known as the Python asynchronous processing scheme.

The back of the clock will overwrite the previous clock, a process with only one suspended clock

The understanding of nouns:

Async: The program takes advantage of kernel functionality in execution to help perform the necessary ancillary operations without impacting the application layer's ongoing execution

Note: The synchronous and asynchronous mechanisms here are relative to many processes. (Note that the asynchronous execution of multithreading is distinguished, and then I write the article about the asynchronous mechanism of multithreading)

In the process of communication this concept, only Singal is executed asynchronously, in addition to the python process of communication between the mechanism of pipe (pipeline), queue (queuing), value (shared space) and so on

Have the opportunity to compare these mechanisms in a different

(3) Signal.pause ()

Blocks the process and waits for a signal. Stops blocking when the signal is received

For example: Wait for the signal () function to be sent

(4) signal.signal (Sig,handler)

Operating system cores/processes emit processing signals when a failure occurs during process operation or when interprocess communication is required

Parameter understanding:

Signal name to be processed by SIG

Handler signal processing method Optional value: SIG_DFL means the default method processing

Sig_ign indicates that this signal is ignored ( typically used to avoid interaction between parent and child processes )

Func Custom Function (actually a class of callback function, when the signal occurs, call the handler function, after completion, go back to the original location to continue to execute the following program)

Custom Function Format: (no difference from defining python normal functions)

def func (Sig,frame):

SIG: Received Signal

Frame: Signal Structure object (can view signal information through structure object, basic use)

The signal function is actually an asynchronous handler, and whenever the function is executed, the process receives the corresponding signal at any time to process

Asynchronous here is the above mentioned asynchronous mechanism, is the computer kernel program and this process running simultaneously, do not interfere with each other a mechanism, for the normal implementation of the process has a key role.

This asynchronous mechanism exists in any back-end programming language, but is not implemented in the same way as the details.

So how do singnal use it?

The general signal signal is defined at the beginning of the program that needs to detect the exception, and when the program runs down, it captures the signal or other processes emitted by the operating system signal

The current state of the program is stopped and the captured signal is processed immediately.

Python's signal signal

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: 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.