| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{signal}} | 
|  | 2 |  | 
|  | 3 | \bimodindex{signal} | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 4 | This module provides mechanisms to use signal handlers in Python. | 
|  | 5 | Some general rules for working with signals handlers: | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 6 |  | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 7 | \begin{itemize} | 
|  | 8 |  | 
|  | 9 | \item | 
|  | 10 | A handler for a particular signal, once set, remains installed until | 
| Guido van Rossum | c171552 | 1996-02-12 23:18:51 +0000 | [diff] [blame^] | 11 | it is explicitly reset (i.e. Python emulates the BSD style interface | 
|  | 12 | regardless of the underlying implementation), with the exception of | 
|  | 13 | the handler for \code{SIGCHLD}, which follows the underlying | 
|  | 14 | implementation. | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 15 |  | 
|  | 16 | \item | 
|  | 17 | There is no way to ``block'' signals temporarily from critical | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 18 | sections (since this is not supported by all \UNIX{} flavors). | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 19 |  | 
|  | 20 | \item | 
|  | 21 | Although Python signal handlers are called asynchronously as far as | 
|  | 22 | the Python user is concerned, they can only occur between the | 
|  | 23 | ``atomic'' instructions of the Python interpreter.  This means that | 
|  | 24 | signals arriving during long calculations implemented purely in C | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 25 | (e.g.\ regular expression matches on large bodies of text) may be | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 26 | delayed for an arbitrary amount of time. | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 27 |  | 
|  | 28 | \item | 
|  | 29 | When a signal arrives during an I/O operation, it is possible that the | 
|  | 30 | I/O operation raises an exception after the signal handler returns. | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 31 | This is dependent on the underlying \UNIX{} system's semantics regarding | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 32 | interrupted system calls. | 
|  | 33 |  | 
|  | 34 | \item | 
|  | 35 | Because the C signal handler always returns, it makes little sense to | 
|  | 36 | catch synchronous errors like \code{SIGFPE} or \code{SIGSEGV}. | 
|  | 37 |  | 
|  | 38 | \item | 
|  | 39 | Python installs a small number of signal handlers by default: | 
|  | 40 | \code{SIGPIPE} is ignored (so write errors on pipes and sockets can be | 
|  | 41 | reported as ordinary Python exceptions), \code{SIGINT} is translated | 
|  | 42 | into a \code{KeyboardInterrupt} exception, and \code{SIGTERM} is | 
|  | 43 | caught so that necessary cleanup (especially \code{sys.exitfunc}) can | 
|  | 44 | be performed before actually terminating.  All of these can be | 
|  | 45 | overridden. | 
|  | 46 |  | 
|  | 47 | \item | 
|  | 48 | Some care must be taken if both signals and threads are used in the | 
|  | 49 | same program.  The fundamental thing to remember in using signals and | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 50 | threads simultaneously is:\ always perform \code{signal()} operations | 
|  | 51 | in the main thread of execution.  Any thread can perform an | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 52 | \code{alarm()}, \code{getsignal()}, or \code{pause()}; only the main | 
|  | 53 | thread can set a new signal handler, and the main thread will be the | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 54 | only one to receive signals (this is enforced by the Python signal | 
|  | 55 | module, even if the underlying thread implementation supports sending | 
|  | 56 | signals to individual threads).  This means that signals can't be used | 
|  | 57 | as a means of interthread communication.  Use locks instead. | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | \end{itemize} | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 60 |  | 
|  | 61 | The variables defined in the signal module are: | 
|  | 62 |  | 
|  | 63 | \renewcommand{\indexsubitem}{(in module signal)} | 
|  | 64 | \begin{datadesc}{SIG_DFL} | 
|  | 65 | This is one of two standard signal handling options; it will simply | 
|  | 66 | perform the default function for the signal.  For example, on most | 
|  | 67 | systems the default action for SIGQUIT is to dump core and exit, | 
|  | 68 | while the default action for SIGCLD is to simply ignore it. | 
|  | 69 | \end{datadesc} | 
|  | 70 |  | 
|  | 71 | \begin{datadesc}{SIG_IGN} | 
|  | 72 | This is another standard signal handler, which will simply ignore | 
|  | 73 | the given signal. | 
|  | 74 | \end{datadesc} | 
|  | 75 |  | 
|  | 76 | \begin{datadesc}{SIG*} | 
|  | 77 | All the signal numbers are defined symbolically.  For example, the | 
|  | 78 | hangup signal is defined as \code{signal.SIGHUP}; the variable names | 
|  | 79 | are identical to the names used in C programs, as found in | 
|  | 80 | \file{signal.h}. | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 81 | The \UNIX{} man page for \file{signal} lists the existing signals (on | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 82 | some systems this is \file{signal(2)}, on others the list is in | 
|  | 83 | \file{signal(7)}). | 
|  | 84 | Note that not all systems define the same set of signal names; only | 
|  | 85 | those names defined by the system are defined by this module. | 
|  | 86 | \end{datadesc} | 
|  | 87 |  | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 88 | \begin{datadesc}{NSIG} | 
|  | 89 | One more than the number of the highest signal number. | 
|  | 90 | \end{datadesc} | 
|  | 91 |  | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 92 | The signal module defines the following functions: | 
|  | 93 |  | 
|  | 94 | \begin{funcdesc}{alarm}{time} | 
|  | 95 | If \var{time} is non-zero, this function requests that a | 
|  | 96 | \code{SIGALRM} signal be sent to the process in \var{time} seconds. | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 97 | Any previously scheduled alarm is canceled (i.e.\ only one alarm can | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 98 | be scheduled at any time).  The returned value is then the number of | 
|  | 99 | seconds before any previously set alarm was to have been delivered. | 
|  | 100 | If \var{time} is zero, no alarm id scheduled, and any scheduled | 
|  | 101 | alarm is canceled.  The return value is the number of seconds | 
|  | 102 | remaining before a previously scheduled alarm.  If the return value | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 103 | is zero, no alarm is currently scheduled.  (See the \UNIX{} man page | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 104 | \code{alarm(2)}.) | 
|  | 105 | \end{funcdesc} | 
|  | 106 |  | 
|  | 107 | \begin{funcdesc}{getsignal}{signalnum} | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 108 | Return the current signal handler for the signal \var{signalnum}. | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 109 | The returned value may be a callable Python object, or one of the | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 110 | special values \code{signal.SIG_IGN}, \code{signal.SIG_DFL} or | 
|  | 111 | \code{None}.  Here, \code{signal.SIG_IGN} means that the signal was | 
|  | 112 | previously ignored, \code{signal.SIG_DFL} means that the default way | 
|  | 113 | of handling the signal was previously in use, and \code{None} means | 
|  | 114 | that the previous signal handler was not installed from Python. | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 115 | \end{funcdesc} | 
|  | 116 |  | 
|  | 117 | \begin{funcdesc}{pause}{} | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 118 | Cause the process to sleep until a signal is received; the | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 119 | appropriate handler will then be called.  Returns nothing.  (See the | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 120 | \UNIX{} man page \code{signal(2)}.) | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 121 | \end{funcdesc} | 
|  | 122 |  | 
|  | 123 | \begin{funcdesc}{signal}{signalnum\, handler} | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 124 | Set the handler for signal \var{signalnum} to the function | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 125 | \var{handler}.  \var{handler} can be any callable Python object, or | 
|  | 126 | one of the special values \code{signal.SIG_IGN} or | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 127 | \code{signal.SIG_DFL}.  The previous signal handler will be returned | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 128 | (see the description of \code{getsignal()} above).  (See the \UNIX{} | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 129 | man page \code{signal(2)}.) | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 130 |  | 
| Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 131 | When threads are enabled, this function can only be called from the | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 132 | main thread; attempting to call it from other threads will cause a | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 133 | \code{ValueError} exception to be raised. | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 134 |  | 
|  | 135 | The \var{handler} is called with two arguments: the signal number | 
|  | 136 | and the current stack frame (\code{None} or a frame object; see the | 
|  | 137 | reference manual for a description of frame objects). | 
|  | 138 | \obindex{frame} | 
| Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 139 | \end{funcdesc} |