blob: 144b054dba21df1ecb601d2de764d27e50985e20 [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
21There is no way to ``block'' signals temporarily from critical
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000022sections (since this is not supported by all \UNIX{} flavors).
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000023
24\item
25Although Python signal handlers are called asynchronously as far as
26the Python user is concerned, they can only occur between the
27``atomic'' instructions of the Python interpreter. This means that
Fred Drake907e76b2001-07-06 20:30:11 +000028signals arriving during long calculations implemented purely in C
29(such as regular expression matches on large bodies of text) may be
Guido van Rossum470be141995-03-17 16:07:09 +000030delayed for an arbitrary amount of time.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000031
32\item
33When a signal arrives during an I/O operation, it is possible that the
34I/O operation raises an exception after the signal handler returns.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000035This is dependent on the underlying \UNIX{} system's semantics regarding
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000036interrupted system calls.
37
38\item
Fred Drake55f44921998-01-22 15:56:41 +000039Because the \C{} signal handler always returns, it makes little sense to
40catch synchronous errors like \constant{SIGFPE} or \constant{SIGSEGV}.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000041
42\item
43Python installs a small number of signal handlers by default:
Fred Drake55f44921998-01-22 15:56:41 +000044\constant{SIGPIPE} is ignored (so write errors on pipes and sockets can be
Guido van Rossum5fc9c861999-03-25 20:30:00 +000045reported as ordinary Python exceptions) and \constant{SIGINT} is translated
46into a \exception{KeyboardInterrupt} exception. All of these can be
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000047overridden.
48
49\item
50Some care must be taken if both signals and threads are used in the
51same program. The fundamental thing to remember in using signals and
Fred Drake55f44921998-01-22 15:56:41 +000052threads simultaneously is:\ always perform \function{signal()} operations
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000053in the main thread of execution. Any thread can perform an
Fred Drake55f44921998-01-22 15:56:41 +000054\function{alarm()}, \function{getsignal()}, or \function{pause()};
55only the main thread can set a new signal handler, and the main thread
56will be the only one to receive signals (this is enforced by the
57Python \module{signal} module, even if the underlying thread
58implementation supports sending signals to individual threads). This
Thomas Woutersf8316632000-07-16 19:01:10 +000059means that signals can't be used as a means of inter-thread
Fred Drake55f44921998-01-22 15:56:41 +000060communication. Use locks instead.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000061
62\end{itemize}
Guido van Rossum626c1e71995-02-07 14:37:02 +000063
Fred Drake55f44921998-01-22 15:56:41 +000064The variables defined in the \module{signal} module are:
Guido van Rossum626c1e71995-02-07 14:37:02 +000065
Guido van Rossum626c1e71995-02-07 14:37:02 +000066\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 Drake55f44921998-01-22 15:56:41 +000069 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 Rossum626c1e71995-02-07 14:37:02 +000072\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 Drake55f44921998-01-22 15:56:41 +000081 hangup signal is defined as \constant{signal.SIGHUP}; the variable names
Guido van Rossum626c1e71995-02-07 14:37:02 +000082 are identical to the names used in C programs, as found in
Fred Drakea5aefba1998-08-14 17:05:17 +000083 \code{<signal.h>}.
Fred Drake55f44921998-01-22 15:56:41 +000084 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 Rossum626c1e71995-02-07 14:37:02 +000087 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 Rossume1ff7ad1995-02-15 15:52:32 +000091\begin{datadesc}{NSIG}
92 One more than the number of the highest signal number.
93\end{datadesc}
94
Fred Drake55f44921998-01-22 15:56:41 +000095The \module{signal} module defines the following functions:
Guido van Rossum626c1e71995-02-07 14:37:02 +000096
97\begin{funcdesc}{alarm}{time}
98 If \var{time} is non-zero, this function requests that a
Fred Drake55f44921998-01-22 15:56:41 +000099 \constant{SIGALRM} signal be sent to the process in \var{time} seconds.
Fred Drake907e76b2001-07-06 20:30:11 +0000100 Any previously scheduled alarm is canceled (only one alarm can
Guido van Rossum626c1e71995-02-07 14:37:02 +0000101 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
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000106 is zero, no alarm is currently scheduled. (See the \UNIX{} man page
Fred Drake55f44921998-01-22 15:56:41 +0000107 \manpage{alarm}{2}.)
Guido van Rossum626c1e71995-02-07 14:37:02 +0000108\end{funcdesc}
109
110\begin{funcdesc}{getsignal}{signalnum}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000111 Return the current signal handler for the signal \var{signalnum}.
Guido van Rossum626c1e71995-02-07 14:37:02 +0000112 The returned value may be a callable Python object, or one of the
Fred Drake55f44921998-01-22 15:56:41 +0000113 special values \constant{signal.SIG_IGN}, \constant{signal.SIG_DFL} or
114 \constant{None}. Here, \constant{signal.SIG_IGN} means that the
115 signal was previously ignored, \constant{signal.SIG_DFL} means that the
116 default way of handling the signal was previously in use, and
117 \code{None} means that the previous signal handler was not installed
118 from Python.
Guido van Rossum626c1e71995-02-07 14:37:02 +0000119\end{funcdesc}
120
121\begin{funcdesc}{pause}{}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000122 Cause the process to sleep until a signal is received; the
Guido van Rossum626c1e71995-02-07 14:37:02 +0000123 appropriate handler will then be called. Returns nothing. (See the
Fred Drake55f44921998-01-22 15:56:41 +0000124 \UNIX{} man page \manpage{signal}{2}.)
Guido van Rossum626c1e71995-02-07 14:37:02 +0000125\end{funcdesc}
126
Fred Drakecce10901998-03-17 06:33:25 +0000127\begin{funcdesc}{signal}{signalnum, handler}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000128 Set the handler for signal \var{signalnum} to the function
Guido van Rossum52481481998-06-09 15:42:25 +0000129 \var{handler}. \var{handler} can be a callable Python object
130 taking two arguments (see below), or
Fred Drake55f44921998-01-22 15:56:41 +0000131 one of the special values \constant{signal.SIG_IGN} or
132 \constant{signal.SIG_DFL}. The previous signal handler will be returned
133 (see the description of \function{getsignal()} above). (See the
134 \UNIX{} man page \manpage{signal}{2}.)
Guido van Rossum626c1e71995-02-07 14:37:02 +0000135
Guido van Rossume1ff7ad1995-02-15 15:52:32 +0000136 When threads are enabled, this function can only be called from the
Guido van Rossum626c1e71995-02-07 14:37:02 +0000137 main thread; attempting to call it from other threads will cause a
Fred Drake55f44921998-01-22 15:56:41 +0000138 \exception{ValueError} exception to be raised.
Guido van Rossum470be141995-03-17 16:07:09 +0000139
140 The \var{handler} is called with two arguments: the signal number
141 and the current stack frame (\code{None} or a frame object; see the
142 reference manual for a description of frame objects).
143\obindex{frame}
Guido van Rossum626c1e71995-02-07 14:37:02 +0000144\end{funcdesc}
Andrew M. Kuchling42db27f1998-08-18 19:38:54 +0000145
146\subsection{Example}
147\nodename{Signal Example}
148
149Here is a minimal example program. It uses the \function{alarm()}
150function to limit the time spent waiting to open a file; this is
151useful if the file is for a serial device that may not be turned on,
152which would normally cause the \function{os.open()} to hang
153indefinitely. The solution is to set a 5-second alarm before opening
154the file; if the operation takes too long, the alarm signal will be
155sent, and the handler raises an exception.
156
157\begin{verbatim}
Fred Drake791c3512001-05-10 15:57:17 +0000158import signal, os
Andrew M. Kuchling42db27f1998-08-18 19:38:54 +0000159
160def handler(signum, frame):
161 print 'Signal handler called with signal', signum
162 raise IOError, "Couldn't open device!"
163
164# Set the signal handler and a 5-second alarm
165signal.signal(signal.SIGALRM, handler)
166signal.alarm(5)
167
168# This open() may hang indefinitely
Fred Drake791c3512001-05-10 15:57:17 +0000169fd = os.open('/dev/ttyS0', os.O_RDWR)
Andrew M. Kuchling42db27f1998-08-18 19:38:54 +0000170
171signal.alarm(0) # Disable the alarm
172\end{verbatim}