Python Linux signal programming signals, pythonsignals

Source: Internet
Author: User
Tags signal handler

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.

  • Signal allows asynchronous processing of events.
  • The SIGKILL signal is being sent.Kill-9The signal generated.
  • It is not difficult to use signal programming.

    EnterMan 7 signalView the type of signal:

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, usePsCommand to list the process list, find its PID, and Kill it.

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.