blob: 30293858524f5d87d750095940c305e6a2cbacf0 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{signal} ---
Fred Drakef8ca7d82000-10-10 17:03:45 +00002 Set handlers for asynchronous events}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{builtin}{signal}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Set handlers for asynchronous events.}
6
Fred Drakef8ca7d82000-10-10 17:03:45 +00007
Guido van Rossume1ff7ad1995-02-15 15:52:32 +00008This module provides mechanisms to use signal handlers in Python.
Andrew M. Kuchling42db27f1998-08-18 19:38:54 +00009Some general rules for working with signals and their handlers:
Guido van Rossum626c1e71995-02-07 14:37:02 +000010
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000011\begin{itemize}
12
13\item
14A handler for a particular signal, once set, remains installed until
Fred Drake907e76b2001-07-06 20:30:11 +000015it is explicitly reset (Python emulates the BSD style interface
Guido van Rossumc1715521996-02-12 23:18:51 +000016regardless of the underlying implementation), with the exception of
Fred Drake55f44921998-01-22 15:56:41 +000017the handler for \constant{SIGCHLD}, which follows the underlying
Guido van Rossumc1715521996-02-12 23:18:51 +000018implementation.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000019
20\item
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000021Although Python signal handlers are called asynchronously as far as
22the Python user is concerned, they can only occur between the
23``atomic'' instructions of the Python interpreter. This means that
Fred Drake907e76b2001-07-06 20:30:11 +000024signals arriving during long calculations implemented purely in C
25(such as regular expression matches on large bodies of text) may be
Guido van Rossum470be141995-03-17 16:07:09 +000026delayed for an arbitrary amount of time.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000027
28\item
29When a signal arrives during an I/O operation, it is possible that the
30I/O operation raises an exception after the signal handler returns.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000031This is dependent on the underlying \UNIX{} system's semantics regarding
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000032interrupted system calls.
33
34\item
Fred Drake55f44921998-01-22 15:56:41 +000035Because the \C{} signal handler always returns, it makes little sense to
36catch synchronous errors like \constant{SIGFPE} or \constant{SIGSEGV}.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000037
38\item
39Python installs a small number of signal handlers by default:
Fred Drake55f44921998-01-22 15:56:41 +000040\constant{SIGPIPE} is ignored (so write errors on pipes and sockets can be
Guido van Rossum5fc9c861999-03-25 20:30:00 +000041reported as ordinary Python exceptions) and \constant{SIGINT} is translated
42into a \exception{KeyboardInterrupt} exception. All of these can be
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000043overridden.
44
45\item
46Some care must be taken if both signals and threads are used in the
47same program. The fundamental thing to remember in using signals and
Fred Drake55f44921998-01-22 15:56:41 +000048threads simultaneously is:\ always perform \function{signal()} operations
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000049in the main thread of execution. Any thread can perform an
Fred Drake55f44921998-01-22 15:56:41 +000050\function{alarm()}, \function{getsignal()}, or \function{pause()};
51only the main thread can set a new signal handler, and the main thread
52will be the only one to receive signals (this is enforced by the
53Python \module{signal} module, even if the underlying thread
54implementation supports sending signals to individual threads). This
Thomas Woutersf8316632000-07-16 19:01:10 +000055means that signals can't be used as a means of inter-thread
Fred Drake55f44921998-01-22 15:56:41 +000056communication. Use locks instead.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000057
58\end{itemize}
Guido van Rossum626c1e71995-02-07 14:37:02 +000059
Fred Drake55f44921998-01-22 15:56:41 +000060The variables defined in the \module{signal} module are:
Guido van Rossum626c1e71995-02-07 14:37:02 +000061
Guido van Rossum626c1e71995-02-07 14:37:02 +000062\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 Drake55f44921998-01-22 15:56:41 +000065 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 Rossum626c1e71995-02-07 14:37:02 +000068\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 Drake55f44921998-01-22 15:56:41 +000077 hangup signal is defined as \constant{signal.SIGHUP}; the variable names
Guido van Rossum626c1e71995-02-07 14:37:02 +000078 are identical to the names used in C programs, as found in
Fred Drakea5aefba1998-08-14 17:05:17 +000079 \code{<signal.h>}.
Fred Drake55f44921998-01-22 15:56:41 +000080 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 Rossum626c1e71995-02-07 14:37:02 +000083 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 Rossume1ff7ad1995-02-15 15:52:32 +000087\begin{datadesc}{NSIG}
88 One more than the number of the highest signal number.
89\end{datadesc}
90
Michael W. Hudson34f20ea2002-05-27 15:08:24 +000091\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 Drake55f44921998-01-22 15:56:41 +0000101The \module{signal} module defines the following functions:
Guido van Rossum626c1e71995-02-07 14:37:02 +0000102
103\begin{funcdesc}{alarm}{time}
104 If \var{time} is non-zero, this function requests that a
Fred Drake55f44921998-01-22 15:56:41 +0000105 \constant{SIGALRM} signal be sent to the process in \var{time} seconds.
Fred Drake907e76b2001-07-06 20:30:11 +0000106 Any previously scheduled alarm is canceled (only one alarm can
Guido van Rossum626c1e71995-02-07 14:37:02 +0000107 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 Drakee73ad2a2002-02-15 20:59:43 +0000112 is zero, no alarm is currently scheduled. (See the \UNIX{} man page
113 \manpage{alarm}{2}.)
114 Availability: \UNIX.
Guido van Rossum626c1e71995-02-07 14:37:02 +0000115\end{funcdesc}
116
117\begin{funcdesc}{getsignal}{signalnum}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000118 Return the current signal handler for the signal \var{signalnum}.
Guido van Rossum626c1e71995-02-07 14:37:02 +0000119 The returned value may be a callable Python object, or one of the
Fred Drake55f44921998-01-22 15:56:41 +0000120 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 Rossum626c1e71995-02-07 14:37:02 +0000126\end{funcdesc}
127
128\begin{funcdesc}{pause}{}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000129 Cause the process to sleep until a signal is received; the
Skip Montanaro8d1fdaf2002-02-15 20:36:19 +0000130 appropriate handler will then be called. Returns nothing. Not on
131 Windows. (See the \UNIX{} man page \manpage{signal}{2}.)
Guido van Rossum626c1e71995-02-07 14:37:02 +0000132\end{funcdesc}
133
Fred Drakecce10901998-03-17 06:33:25 +0000134\begin{funcdesc}{signal}{signalnum, handler}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000135 Set the handler for signal \var{signalnum} to the function
Guido van Rossum52481481998-06-09 15:42:25 +0000136 \var{handler}. \var{handler} can be a callable Python object
137 taking two arguments (see below), or
Fred Drake55f44921998-01-22 15:56:41 +0000138 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 Rossum626c1e71995-02-07 14:37:02 +0000142
Guido van Rossume1ff7ad1995-02-15 15:52:32 +0000143 When threads are enabled, this function can only be called from the
Guido van Rossum626c1e71995-02-07 14:37:02 +0000144 main thread; attempting to call it from other threads will cause a
Fred Drake55f44921998-01-22 15:56:41 +0000145 \exception{ValueError} exception to be raised.
Guido van Rossum470be141995-03-17 16:07:09 +0000146
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 Rossum626c1e71995-02-07 14:37:02 +0000151\end{funcdesc}
Andrew M. Kuchling42db27f1998-08-18 19:38:54 +0000152
Michael W. Hudson34f20ea2002-05-27 15:08:24 +0000153The following functions are supported if your platform does. Most
154modern \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. Kuchling42db27f1998-08-18 19:38:54 +0000193\subsection{Example}
194\nodename{Signal Example}
195
196Here is a minimal example program. It uses the \function{alarm()}
197function to limit the time spent waiting to open a file; this is
198useful if the file is for a serial device that may not be turned on,
199which would normally cause the \function{os.open()} to hang
200indefinitely. The solution is to set a 5-second alarm before opening
201the file; if the operation takes too long, the alarm signal will be
202sent, and the handler raises an exception.
203
204\begin{verbatim}
Fred Drake791c3512001-05-10 15:57:17 +0000205import signal, os
Andrew M. Kuchling42db27f1998-08-18 19:38:54 +0000206
207def 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
212signal.signal(signal.SIGALRM, handler)
213signal.alarm(5)
214
215# This open() may hang indefinitely
Fred Drake791c3512001-05-10 15:57:17 +0000216fd = os.open('/dev/ttyS0', os.O_RDWR)
Andrew M. Kuchling42db27f1998-08-18 19:38:54 +0000217
218signal.alarm(0) # Disable the alarm
219\end{verbatim}