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