Python Signal processing module signal

Source: Internet
Author: User
Tags semaphore sigint signal

Python Signal processing module signal

Python's module for signal processing mainly uses the signal module, but the signal is primarily for UNIX systems, so Python does not perform well on the Windows platform for signal processing.

To see the semaphore in Python, you can use DIR (signal) to view it.

Signal.signal ()

In the signal module, the main use of the signal.signal () function to preset the signal processing function

singnal.signal(signalnum, handler)

The first parameter is the semaphore, the second parameter signal processing function.

Let's look at a simple example where

    • Defines a signal processing function, Signal_handler (), to handle the operation performed when the program receives a signal
    • A loop waits for a signal to be sent
#!/usr/bin/env python#-*-Coding:utf-8-*-ImportSignalImportTime def signal_handler(Signum, frame):Print' Received signal: ', Signum) while True: Signal.signal (signal. SIGHUP, Signal_handler)# 1Signal.signal (signal. SIGINT, Signal_handler)# 2Signal.signal (signal. Sigquit, Signal_handler)# 3Signal.signal (signal. SIGALRM, Signal_handler)#Signal.signal (signal. SIGTERM, Signal_handler)#Signal.signal (signal. Sigcont, Signal_handler)#     while True: Print (' Waiting ') Time.sleep (1)

Run the above program

python test.py

Then open a different terminal, find the corresponding process, and perform the following kill operation

kill -1 <pid>kill -2 <pid>kill -3 <pid>kill -14 <pid>kill -15 <pid>kill -18 <pid>kill -9# 最后杀死进程

At this point, you can see the output of the test.py, printing is the specific received signal.

One thing to note here is that the SIGINT signal is registered in the program, so using CTRL + C after running the program does not end the process, but still the signal received by the printing process.

Signal.alarm ()

In addition, the signal module provides a useful function signal.alarm (), which is used to send a SIGALRM signal to the process itself after a certain amount of time, such as the following example setting to send itself a SIGALRM signal after 5 seconds.

#!/usr/bin/env python# -*- coding: utf-8 -*-import signalimport timedef signal_handler(signum, frame):    print(‘Received signal: ‘, signum)whileTrue:    # 14    signal.alarm(5)    whileTrue:        print(‘waiting‘)        time.sleep(1)
Reference
    • Http://man7.org/linux/man-pages/man7/signal.7.html
    • Https://docs.python.org/2/library/signal.html#module-signal

Please indicate this address in the form of a link.
This address: http://blog.csdn.net/kongxx/article/details/50976802

Python Signal processing 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.