Python--signal

Source: Internet
Author: User
Tags signal handler

This module provides support for using a signal processing handle in Python. Here are some things to keep in mind when using signals and their handles:

  • In addition to the handle of the signal SIGCHLD in accordance with the underlying implementation, a handle specifically for a signal once set, unless explicitly reset, will remain set state. (Python simulates the BSD-style interface, regardless of the underlying implementation).
  • You cannot try to temporarily block the signal from critical sections, because UNIX's style does not support this.
  • Although Python's signal handles are called asynchronous from the perspective of the Python user, they can only appear between the "atom" instructions of the Python interpreter. In other words, for example, when a long operation in pure C is in progress and there is a signal arriving, the signal may be delayed for any length of time.
  • When the signal arrives during an I/O operation, the I/O operation may throw an exception after the signal handle returns. This relies on the semantics of the underlying Unix about the interrupted system call.
  • Because C's signal handle always has a return value, capturing a synchronous error, such as SIGFPE or SIGSEGV, has little meaning.
  • Python will set some signal handles by default:   sigpipe   is ignored (so write errors on pipelines and sockets can be reported as normal Python exceptions),   sigint   was turned into a  keyboardinterrupt   Exceptions. But these can all be covered.
  • if both threads and signals are in the same program, be careful: Always execute  signal ()   operation. Any thread can execute  alarm () ,  getsignal () ,  pause () , setitimer ()   or  getitimer () ; Only the main thread can set a new signal handle, and only the main thread can receive the signal (Python's  signal   module is so specified, regardless of whether the underlying thread implementation allows signaling to a separate thread. This means that the signal cannot be used to act as a means of communication between threads, and use locks in python.

  

Variables in the signal module:

signal. SIG_DFL
  
This is one of the two standard signal handle options and will execute the default function set for the signal. For example, the default action for Sigquit on most systems is the dump kernel and then exits, while the default action for SIGCHLD is to ignore it directly.
  
signal. Sig_ign
  
This is another of the two standard signal handle options, which directly ignores the specified signal.
  
sig*
  
all signal numbers are defined in the form of symbolic constants, for example, a pending signal is defined as Signal. SIGHUP, the variable name is the same as the one used in the C program and can be found in the <signal.h> header file. Unix's man page that corresponds to 'signal ()' lists all the defined signals(some are signal (2) and some are signal (7) on the system). ). Note Not all systems are defined with the same signal name.
  
signal. Ctrl_c_event
  
corresponds to CTRL + C keystroke event, this signal can only be used with Os.kill ().
  Applies to: Windows.
   Python 2.7 Introduction
  
signal. Ctrl_break_event
  
   corresponding to the Ctrl+break keystroke operation, this signal can only be used with Os.kill ().
  Applies to: Windows.
   Python 2.7 Introduction
  
signal. NSIG
  
Just like the maximum signal value is 1 larger.
  
signal. Itimer_real
  
  Decrements interval timer in real time, and delivers SIGALRM upon expiration.
  
signal. Itimer_virtual
  
  Decrements interval Timer Only if the process is executing, and delivers sigvtalrm upon expiration.
  
signal. Itimer_prof
decrements interval time R both when the process executes and, the system is executing on behalf of the process. Coupled with itimer_virtual, this timer was usually used to profiles the time spent by the application in user and kernel SP Ace. Sigprof is delivered upon expiration.


signal   Module exceptions
signal. Itimererror
  
Thrown from the underlying Setitimer () or Getitimer () to flag an error. The exception is thrown when an invalid timer or negative time is passed into Setitimer (), which is a subclass of IOError.

Functions of the signal module

Signal.alarm (Time)

  

If the parameter time is nonzero, the function requests a SIGALRM signal to be sent to the process within time. All previously scheduled alarm are canceled (only one alarm can be scheduled at a time). The return value is the number of seconds remaining in the previously set alarm. If time is zero, no alarm will be dispatched, and all alarm that have been dispatched will be canceled. If the return value is zero, the alarm is not currently scheduled. (View the Unix man manual Alarm (2) .)

Applies To: Unix

  

signal.getsignal (signalnum)

  

signalnum Signal. Sig_ign,signal. One of the SIG_DFL or None. Here signal. Sig_ign means that the signal was previously ignored and signal. SIG_DFL means that the signal was previously processed by default, none means that the previous signal handle is not set.

  

Signal.pause ()

causes the process to sleep until the signal arrives, the appropriate handle is called, there is no return value, and Windows is not supported. (View the Unix Man manual Signal (2)).

  

Signal.setitimer (which, seconds[, interval])

  

which the parameter to the specified timer (signal. Itimer_real,signal. Itimer_virtualorsignal. One of the Itimer_prof) is set to a parameter secondsseconds after the trigger (float type can also, unlikeAlarm ()), and then everyintervalseconds to trigger once. By setting seconds to 0, you can clear thewhichthe specified timer.

When a timer is violated, a signal is sent to the process, and the signal transmitted depends on the timer used, signal. Itimer_real will send sigalrm, signal. Itimer_virtual send sigvtalrm, signal. Itimer_prof send sigprof.

  The old value is returned as a tuple: (delay, interval).

Passing in an invalid timer can cause a itimererror

Applies to: Unix.

  Python 2.6 Introduction

  

Signal.getitimer (which)

Returns the current value of the timer specified by the parameter which .

Applies to: Unix.

  Python 2.6 Introduction

  

signal.set_wakeup_fd (FD)

Sets the wake-up file descriptor to parameter FD. when a signal is received, a ' bytes ' byte is written to the specified FD. Can be used by a library to wake up a poll or select call, allowing the signal to be fully processed.

returns the old file description word, parameter The FD must be non-blocking and whether to clear the byte record before calling poll or select again depends on the specific library.

When a thread is allowed, the function can only be called by the main thread, and other threads calling it will throw a valueerror exception.

  Python 2.6 Introduction

  

signal.siginterrupt (signalnum, flag)

Change system call Restart rule: If flag is False, the system call will be restarted when the signal signalnum is interrupted, or the system call will be interrupted. no return value.

Applies To: Unix (see siginterrupt (3) for Man manual)

Note When using Signal () setting a signal handle will be interrupted by an implicit call to Siginterrupt () to reset the restart behavior, that is, flag is true for the specified signal.

  Python 2.6 Introduction

signal.signal (Signalnum, handler)

  

Sets the handle of the signal signalnum to function handler. Handler can be a Python callable object that accepts two parameters, or signal. Sig_ign and signal. One of the SIG_DFL. The function will return the previous signal handle,

When a thread is allowed, the function can only be called by the main thread, and other threads calling it will throw a valueerror exception.

Call handle   handler   need to provide two parameters: the value of the signal and the current stack frame   ( none   or a Frame object; check   description in the Type hierarchy   or  inspect The property description in the   module can understand the frame object

  Windows Signal () can only be used with SIGABRT,SIGFPE,sigill,SIGINT,SIGSEGV or SIGTERM called. Otherwise, a valueerror exception is thrown.

Example

Here's an example that uses the alarm () function to limit the time it takes to open a file, which is useful when the file is ready for a continuous device that doesn't have to be opened, which usually causes Os.open () to hang immediately. The solution is to set a 5-second alarm when the file is opened, and when the open file times out, the alarm signal is sent, and the handle throws an exception.

  

Import signal, Osdef handler (Signum, frame):    print ' signal handler called with Signal ', Signum    raise IOError ("Cou LDN ' t open device! ") # Set the signal handler and a 5-second alarmsignal.signal (signal. SIGALRM, Handler) Signal.alarm (5) # This open () could hang INDEFINITELYFD = Os.open ('/dev/ttys0 ', OS. O_RDWR) signal.alarm (0)          # Disable the alarm

 

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