Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{signal} --- |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 2 | Set handlers for asynchronous events} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{signal} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Set handlers for asynchronous events.} |
| 6 | |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 8 | This module provides mechanisms to use signal handlers in Python. |
Andrew M. Kuchling | 42db27f | 1998-08-18 19:38:54 +0000 | [diff] [blame] | 9 | Some general rules for working with signals and their handlers: |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 11 | \begin{itemize} |
| 12 | |
| 13 | \item |
| 14 | A handler for a particular signal, once set, remains installed until |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 15 | it is explicitly reset (Python emulates the BSD style interface |
Guido van Rossum | c171552 | 1996-02-12 23:18:51 +0000 | [diff] [blame] | 16 | regardless of the underlying implementation), with the exception of |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 17 | the handler for \constant{SIGCHLD}, which follows the underlying |
Guido van Rossum | c171552 | 1996-02-12 23:18:51 +0000 | [diff] [blame] | 18 | implementation. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 19 | |
| 20 | \item |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 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 |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 24 | signals arriving during long calculations implemented purely in C |
| 25 | (such as 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 |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 35 | Because the \C{} signal handler always returns, it makes little sense to |
| 36 | catch synchronous errors like \constant{SIGFPE} or \constant{SIGSEGV}. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 37 | |
| 38 | \item |
| 39 | Python installs a small number of signal handlers by default: |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 40 | \constant{SIGPIPE} is ignored (so write errors on pipes and sockets can be |
Guido van Rossum | 5fc9c86 | 1999-03-25 20:30:00 +0000 | [diff] [blame] | 41 | reported as ordinary Python exceptions) and \constant{SIGINT} is translated |
| 42 | into a \exception{KeyboardInterrupt} exception. All of these can be |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 43 | overridden. |
| 44 | |
| 45 | \item |
| 46 | Some care must be taken if both signals and threads are used in the |
| 47 | same program. The fundamental thing to remember in using signals and |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 48 | threads simultaneously is:\ always perform \function{signal()} operations |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 49 | in the main thread of execution. Any thread can perform an |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 50 | \function{alarm()}, \function{getsignal()}, or \function{pause()}; |
| 51 | only the main thread can set a new signal handler, and the main thread |
| 52 | will be the only one to receive signals (this is enforced by the |
| 53 | Python \module{signal} module, even if the underlying thread |
| 54 | implementation supports sending signals to individual threads). This |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 55 | means that signals can't be used as a means of inter-thread |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 56 | communication. Use locks instead. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 57 | |
| 58 | \end{itemize} |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 59 | |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 60 | The variables defined in the \module{signal} module are: |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 62 | \begin{datadesc}{SIG_DFL} |
| 63 | This is one of two standard signal handling options; it will simply |
| 64 | perform the default function for the signal. For example, on most |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 65 | systems the default action for \constant{SIGQUIT} is to dump core |
| 66 | and exit, while the default action for \constant{SIGCLD} is to |
| 67 | simply ignore it. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 68 | \end{datadesc} |
| 69 | |
| 70 | \begin{datadesc}{SIG_IGN} |
| 71 | This is another standard signal handler, which will simply ignore |
| 72 | the given signal. |
| 73 | \end{datadesc} |
| 74 | |
| 75 | \begin{datadesc}{SIG*} |
| 76 | All the signal numbers are defined symbolically. For example, the |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 77 | hangup signal is defined as \constant{signal.SIGHUP}; the variable names |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 78 | are identical to the names used in C programs, as found in |
Fred Drake | a5aefba | 1998-08-14 17:05:17 +0000 | [diff] [blame] | 79 | \code{<signal.h>}. |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 80 | The \UNIX{} man page for `\cfunction{signal()}' lists the existing |
| 81 | signals (on some systems this is \manpage{signal}{2}, on others the |
| 82 | list is in \manpage{signal}{7}). |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 83 | Note that not all systems define the same set of signal names; only |
| 84 | those names defined by the system are defined by this module. |
| 85 | \end{datadesc} |
| 86 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 87 | \begin{datadesc}{NSIG} |
| 88 | One more than the number of the highest signal number. |
| 89 | \end{datadesc} |
| 90 | |
Michael W. Hudson | 34f20ea | 2002-05-27 15:08:24 +0000 | [diff] [blame] | 91 | \begin{datadesc}{SIG_BLOCK} |
| 92 | \end{datadesc} |
| 93 | \begin{datadesc}{SIG_UNBLOCK} |
| 94 | \end{datadesc} |
| 95 | \begin{datadesc}{SIG_SETMASK} |
| 96 | These constants are for use as the first parameter of the |
| 97 | \function{sigprocmask} function described below. |
| 98 | \end{datadesc} |
| 99 | |
| 100 | |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 101 | The \module{signal} module defines the following functions: |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 102 | |
| 103 | \begin{funcdesc}{alarm}{time} |
| 104 | If \var{time} is non-zero, this function requests that a |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 105 | \constant{SIGALRM} signal be sent to the process in \var{time} seconds. |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 106 | Any previously scheduled alarm is canceled (only one alarm can |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 107 | be scheduled at any time). The returned value is then the number of |
| 108 | seconds before any previously set alarm was to have been delivered. |
| 109 | If \var{time} is zero, no alarm id scheduled, and any scheduled |
| 110 | alarm is canceled. The return value is the number of seconds |
| 111 | remaining before a previously scheduled alarm. If the return value |
Fred Drake | e73ad2a | 2002-02-15 20:59:43 +0000 | [diff] [blame] | 112 | is zero, no alarm is currently scheduled. (See the \UNIX{} man page |
| 113 | \manpage{alarm}{2}.) |
| 114 | Availability: \UNIX. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 115 | \end{funcdesc} |
| 116 | |
| 117 | \begin{funcdesc}{getsignal}{signalnum} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 118 | Return the current signal handler for the signal \var{signalnum}. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 119 | The returned value may be a callable Python object, or one of the |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 120 | special values \constant{signal.SIG_IGN}, \constant{signal.SIG_DFL} or |
| 121 | \constant{None}. Here, \constant{signal.SIG_IGN} means that the |
| 122 | signal was previously ignored, \constant{signal.SIG_DFL} means that the |
| 123 | default way of handling the signal was previously in use, and |
| 124 | \code{None} means that the previous signal handler was not installed |
| 125 | from Python. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 126 | \end{funcdesc} |
| 127 | |
| 128 | \begin{funcdesc}{pause}{} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 129 | Cause the process to sleep until a signal is received; the |
Skip Montanaro | 8d1fdaf | 2002-02-15 20:36:19 +0000 | [diff] [blame] | 130 | appropriate handler will then be called. Returns nothing. Not on |
| 131 | Windows. (See the \UNIX{} man page \manpage{signal}{2}.) |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 132 | \end{funcdesc} |
| 133 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 134 | \begin{funcdesc}{signal}{signalnum, handler} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 135 | Set the handler for signal \var{signalnum} to the function |
Guido van Rossum | 5248148 | 1998-06-09 15:42:25 +0000 | [diff] [blame] | 136 | \var{handler}. \var{handler} can be a callable Python object |
| 137 | taking two arguments (see below), or |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 138 | one of the special values \constant{signal.SIG_IGN} or |
| 139 | \constant{signal.SIG_DFL}. The previous signal handler will be returned |
| 140 | (see the description of \function{getsignal()} above). (See the |
| 141 | \UNIX{} man page \manpage{signal}{2}.) |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 143 | 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] | 144 | main thread; attempting to call it from other threads will cause a |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 145 | \exception{ValueError} exception to be raised. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 146 | |
| 147 | The \var{handler} is called with two arguments: the signal number |
| 148 | and the current stack frame (\code{None} or a frame object; see the |
| 149 | reference manual for a description of frame objects). |
| 150 | \obindex{frame} |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 151 | \end{funcdesc} |
Andrew M. Kuchling | 42db27f | 1998-08-18 19:38:54 +0000 | [diff] [blame] | 152 | |
Michael W. Hudson | 34f20ea | 2002-05-27 15:08:24 +0000 | [diff] [blame] | 153 | The following functions are supported if your platform does. Most |
| 154 | modern \UNIX-alikes now do. |
| 155 | |
| 156 | \begin{funcdesc}{sigpending}{} |
| 157 | Return the set of pending signals, i.e. a list containing the |
| 158 | numbers of those signals that have been raised while blocked. |
| 159 | \versionadded{2.3} |
| 160 | \end{funcdesc} |
| 161 | |
| 162 | \begin{funcdesc}{sigprocmask}{how, sigset} |
| 163 | Change the list of currently blocked signals. The parameter |
| 164 | \var{how} should be one of \constant{SIG_BLOCK}, |
| 165 | \constant{SIG_UNBLOCK} or \constant{SIG_SETMASK} and \var{sigset} |
| 166 | should be a sequence of signal numbers. The behaviour of the call |
| 167 | depends on the value of \var{how}: |
| 168 | |
| 169 | \begin{tableii}{l|l}{textrm}{Value of \var{how}}{Behaviour of call} |
| 170 | \lineii{\constant{SIG_BLOCK}} |
| 171 | {The set of blocked signals is the union of the current set |
| 172 | and \var{sigset}.} |
| 173 | \lineii{\constant{SIG_UNBLOCK}} |
| 174 | {The signals in \var{sigset} are removed from the current |
| 175 | set of blocked signals. It is legal to attempt to unblock |
| 176 | a signal which is not blocked.} |
| 177 | \lineii{\constant{SIG_SETMASK}} |
| 178 | {The set of blocked signals is set to the \var{sigset}.} |
| 179 | \end{tableii} |
| 180 | |
| 181 | A list contating the numbers of the previously blocked signals is |
| 182 | returned. |
| 183 | \versionadded{2.3} |
| 184 | \end{funcdesc} |
| 185 | |
| 186 | \begin{funcdesc}{sigsuspend}{sigset} |
| 187 | Temporarily replace the signal mask with \var{sigset} (which should |
| 188 | be a sequnce of signal numbers) and suspend the process until a |
| 189 | signal is received. |
| 190 | \versionadded{2.3} |
| 191 | \end{funcdesc} |
| 192 | |
Andrew M. Kuchling | 42db27f | 1998-08-18 19:38:54 +0000 | [diff] [blame] | 193 | \subsection{Example} |
| 194 | \nodename{Signal Example} |
| 195 | |
| 196 | Here is a minimal example program. It uses the \function{alarm()} |
| 197 | function to limit the time spent waiting to open a file; this is |
| 198 | useful if the file is for a serial device that may not be turned on, |
| 199 | which would normally cause the \function{os.open()} to hang |
| 200 | indefinitely. The solution is to set a 5-second alarm before opening |
| 201 | the file; if the operation takes too long, the alarm signal will be |
| 202 | sent, and the handler raises an exception. |
| 203 | |
| 204 | \begin{verbatim} |
Fred Drake | 791c351 | 2001-05-10 15:57:17 +0000 | [diff] [blame] | 205 | import signal, os |
Andrew M. Kuchling | 42db27f | 1998-08-18 19:38:54 +0000 | [diff] [blame] | 206 | |
| 207 | def handler(signum, frame): |
| 208 | print 'Signal handler called with signal', signum |
| 209 | raise IOError, "Couldn't open device!" |
| 210 | |
| 211 | # Set the signal handler and a 5-second alarm |
| 212 | signal.signal(signal.SIGALRM, handler) |
| 213 | signal.alarm(5) |
| 214 | |
| 215 | # This open() may hang indefinitely |
Fred Drake | 791c351 | 2001-05-10 15:57:17 +0000 | [diff] [blame] | 216 | fd = os.open('/dev/ttyS0', os.O_RDWR) |
Andrew M. Kuchling | 42db27f | 1998-08-18 19:38:54 +0000 | [diff] [blame] | 217 | |
| 218 | signal.alarm(0) # Disable the alarm |
| 219 | \end{verbatim} |