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 |
Michael W. Hudson | 43ed43b | 2003-03-13 13:56:53 +0000 | [diff] [blame] | 21 | There is no way to ``block'' signals temporarily from critical |
| 22 | sections (since this is not supported by all \UNIX{} flavors). |
| 23 | |
| 24 | \item |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 25 | Although Python signal handlers are called asynchronously as far as |
| 26 | the Python user is concerned, they can only occur between the |
| 27 | ``atomic'' instructions of the Python interpreter. This means that |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 28 | signals arriving during long calculations implemented purely in C |
| 29 | (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] | 30 | delayed for an arbitrary amount of time. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 31 | |
| 32 | \item |
| 33 | When a signal arrives during an I/O operation, it is possible that the |
| 34 | I/O operation raises an exception after the signal handler returns. |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 35 | This is dependent on the underlying \UNIX{} system's semantics regarding |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 36 | interrupted system calls. |
| 37 | |
| 38 | \item |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 39 | Because the \C{} signal handler always returns, it makes little sense to |
| 40 | catch synchronous errors like \constant{SIGFPE} or \constant{SIGSEGV}. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 41 | |
| 42 | \item |
| 43 | Python installs a small number of signal handlers by default: |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 44 | \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] | 45 | reported as ordinary Python exceptions) and \constant{SIGINT} is translated |
| 46 | into a \exception{KeyboardInterrupt} exception. All of these can be |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 47 | overridden. |
| 48 | |
| 49 | \item |
| 50 | Some care must be taken if both signals and threads are used in the |
| 51 | same program. The fundamental thing to remember in using signals and |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 52 | threads simultaneously is:\ always perform \function{signal()} operations |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 53 | in the main thread of execution. Any thread can perform an |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 54 | \function{alarm()}, \function{getsignal()}, or \function{pause()}; |
| 55 | only the main thread can set a new signal handler, and the main thread |
| 56 | will be the only one to receive signals (this is enforced by the |
| 57 | Python \module{signal} module, even if the underlying thread |
| 58 | implementation supports sending signals to individual threads). This |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 59 | 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] | 60 | communication. Use locks instead. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 61 | |
| 62 | \end{itemize} |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 63 | |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 64 | The variables defined in the \module{signal} module are: |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 66 | \begin{datadesc}{SIG_DFL} |
| 67 | This is one of two standard signal handling options; it will simply |
| 68 | perform the default function for the signal. For example, on most |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 69 | systems the default action for \constant{SIGQUIT} is to dump core |
| 70 | and exit, while the default action for \constant{SIGCLD} is to |
| 71 | simply ignore it. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 72 | \end{datadesc} |
| 73 | |
| 74 | \begin{datadesc}{SIG_IGN} |
| 75 | This is another standard signal handler, which will simply ignore |
| 76 | the given signal. |
| 77 | \end{datadesc} |
| 78 | |
| 79 | \begin{datadesc}{SIG*} |
| 80 | All the signal numbers are defined symbolically. For example, the |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 81 | hangup signal is defined as \constant{signal.SIGHUP}; the variable names |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 82 | are identical to the names used in C programs, as found in |
Fred Drake | a5aefba | 1998-08-14 17:05:17 +0000 | [diff] [blame] | 83 | \code{<signal.h>}. |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 84 | The \UNIX{} man page for `\cfunction{signal()}' lists the existing |
| 85 | signals (on some systems this is \manpage{signal}{2}, on others the |
| 86 | list is in \manpage{signal}{7}). |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 87 | Note that not all systems define the same set of signal names; only |
| 88 | those names defined by the system are defined by this module. |
| 89 | \end{datadesc} |
| 90 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 91 | \begin{datadesc}{NSIG} |
| 92 | One more than the number of the highest signal number. |
| 93 | \end{datadesc} |
| 94 | |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 95 | The \module{signal} module defines the following functions: |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 96 | |
| 97 | \begin{funcdesc}{alarm}{time} |
| 98 | If \var{time} is non-zero, this function requests that a |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 99 | \constant{SIGALRM} signal be sent to the process in \var{time} seconds. |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 100 | Any previously scheduled alarm is canceled (only one alarm can |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 101 | be scheduled at any time). The returned value is then the number of |
| 102 | seconds before any previously set alarm was to have been delivered. |
| 103 | If \var{time} is zero, no alarm id scheduled, and any scheduled |
| 104 | alarm is canceled. The return value is the number of seconds |
| 105 | remaining before a previously scheduled alarm. If the return value |
Fred Drake | e73ad2a | 2002-02-15 20:59:43 +0000 | [diff] [blame] | 106 | is zero, no alarm is currently scheduled. (See the \UNIX{} man page |
| 107 | \manpage{alarm}{2}.) |
| 108 | Availability: \UNIX. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 109 | \end{funcdesc} |
| 110 | |
| 111 | \begin{funcdesc}{getsignal}{signalnum} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 112 | Return the current signal handler for the signal \var{signalnum}. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 113 | 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] | 114 | special values \constant{signal.SIG_IGN}, \constant{signal.SIG_DFL} or |
| 115 | \constant{None}. Here, \constant{signal.SIG_IGN} means that the |
| 116 | signal was previously ignored, \constant{signal.SIG_DFL} means that the |
| 117 | default way of handling the signal was previously in use, and |
| 118 | \code{None} means that the previous signal handler was not installed |
| 119 | from Python. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 120 | \end{funcdesc} |
| 121 | |
| 122 | \begin{funcdesc}{pause}{} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 123 | Cause the process to sleep until a signal is received; the |
Skip Montanaro | 8d1fdaf | 2002-02-15 20:36:19 +0000 | [diff] [blame] | 124 | appropriate handler will then be called. Returns nothing. Not on |
| 125 | Windows. (See the \UNIX{} man page \manpage{signal}{2}.) |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 126 | \end{funcdesc} |
| 127 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 128 | \begin{funcdesc}{signal}{signalnum, handler} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 129 | Set the handler for signal \var{signalnum} to the function |
Guido van Rossum | 5248148 | 1998-06-09 15:42:25 +0000 | [diff] [blame] | 130 | \var{handler}. \var{handler} can be a callable Python object |
| 131 | taking two arguments (see below), or |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 132 | one of the special values \constant{signal.SIG_IGN} or |
| 133 | \constant{signal.SIG_DFL}. The previous signal handler will be returned |
| 134 | (see the description of \function{getsignal()} above). (See the |
| 135 | \UNIX{} man page \manpage{signal}{2}.) |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 137 | 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] | 138 | main thread; attempting to call it from other threads will cause a |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame] | 139 | \exception{ValueError} exception to be raised. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 140 | |
| 141 | The \var{handler} is called with two arguments: the signal number |
| 142 | and the current stack frame (\code{None} or a frame object; see the |
| 143 | reference manual for a description of frame objects). |
| 144 | \obindex{frame} |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 145 | \end{funcdesc} |
Andrew M. Kuchling | 42db27f | 1998-08-18 19:38:54 +0000 | [diff] [blame] | 146 | |
| 147 | \subsection{Example} |
| 148 | \nodename{Signal Example} |
| 149 | |
| 150 | Here is a minimal example program. It uses the \function{alarm()} |
| 151 | function to limit the time spent waiting to open a file; this is |
| 152 | useful if the file is for a serial device that may not be turned on, |
| 153 | which would normally cause the \function{os.open()} to hang |
| 154 | indefinitely. The solution is to set a 5-second alarm before opening |
| 155 | the file; if the operation takes too long, the alarm signal will be |
| 156 | sent, and the handler raises an exception. |
| 157 | |
| 158 | \begin{verbatim} |
Fred Drake | 791c351 | 2001-05-10 15:57:17 +0000 | [diff] [blame] | 159 | import signal, os |
Andrew M. Kuchling | 42db27f | 1998-08-18 19:38:54 +0000 | [diff] [blame] | 160 | |
| 161 | def handler(signum, frame): |
| 162 | print 'Signal handler called with signal', signum |
| 163 | raise IOError, "Couldn't open device!" |
| 164 | |
| 165 | # Set the signal handler and a 5-second alarm |
| 166 | signal.signal(signal.SIGALRM, handler) |
| 167 | signal.alarm(5) |
| 168 | |
| 169 | # This open() may hang indefinitely |
Fred Drake | 791c351 | 2001-05-10 15:57:17 +0000 | [diff] [blame] | 170 | fd = os.open('/dev/ttyS0', os.O_RDWR) |
Andrew M. Kuchling | 42db27f | 1998-08-18 19:38:54 +0000 | [diff] [blame] | 171 | |
| 172 | signal.alarm(0) # Disable the alarm |
| 173 | \end{verbatim} |