Python Learning note-Signal module signal

Source: Internet
Author: User

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

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.