blob: 63b671f95abfd4a8b7f952b0a4100fe17d486f33 [file] [log] [blame]
Guido van Rossum626c1e71995-02-07 14:37:02 +00001\section{Built-in Module \sectcode{signal}}
2
3\bimodindex{signal}
Guido van Rossume1ff7ad1995-02-15 15:52:32 +00004This module provides mechanisms to use signal handlers in Python.
5Some general rules for working with signals handlers:
Guido van Rossum626c1e71995-02-07 14:37:02 +00006
Guido van Rossume1ff7ad1995-02-15 15:52:32 +00007\begin{itemize}
8
9\item
10A handler for a particular signal, once set, remains installed until
11it is explicitly reset (i.e. Python uses the BSD style interface).
12
13\item
14There is no way to ``block'' signals temporarily from critical
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000015sections (since this is not supported by all \UNIX{} flavors).
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000016
17\item
18Although Python signal handlers are called asynchronously as far as
19the Python user is concerned, they can only occur between the
20``atomic'' instructions of the Python interpreter. This means that
21signals arriving during long calculations implemented purely in C
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000022(e.g.\ regular expression matches on large bodies of text) may be
Guido van Rossum470be141995-03-17 16:07:09 +000023delayed for an arbitrary amount of time.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000024
25\item
26When a signal arrives during an I/O operation, it is possible that the
27I/O operation raises an exception after the signal handler returns.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000028This is dependent on the underlying \UNIX{} system's semantics regarding
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000029interrupted system calls.
30
31\item
32Because the C signal handler always returns, it makes little sense to
33catch synchronous errors like \code{SIGFPE} or \code{SIGSEGV}.
34
35\item
36Python installs a small number of signal handlers by default:
37\code{SIGPIPE} is ignored (so write errors on pipes and sockets can be
38reported as ordinary Python exceptions), \code{SIGINT} is translated
39into a \code{KeyboardInterrupt} exception, and \code{SIGTERM} is
40caught so that necessary cleanup (especially \code{sys.exitfunc}) can
41be performed before actually terminating. All of these can be
42overridden.
43
44\item
45Some care must be taken if both signals and threads are used in the
46same program. The fundamental thing to remember in using signals and
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000047threads simultaneously is:\ always perform \code{signal()} operations
48in the main thread of execution. Any thread can perform an
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000049\code{alarm()}, \code{getsignal()}, or \code{pause()}; only the main
50thread can set a new signal handler, and the main thread will be the
Guido van Rossum470be141995-03-17 16:07:09 +000051only one to receive signals (this is enforced by the Python signal
52module, even if the underlying thread implementation supports sending
53signals to individual threads). This means that signals can't be used
54as a means of interthread communication. Use locks instead.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000055
56\end{itemize}
Guido van Rossum626c1e71995-02-07 14:37:02 +000057
58The variables defined in the signal module are:
59
60\renewcommand{\indexsubitem}{(in module signal)}
61\begin{datadesc}{SIG_DFL}
62 This is one of two standard signal handling options; it will simply
63 perform the default function for the signal. For example, on most
64 systems the default action for SIGQUIT is to dump core and exit,
65 while the default action for SIGCLD is to simply ignore it.
66\end{datadesc}
67
68\begin{datadesc}{SIG_IGN}
69 This is another standard signal handler, which will simply ignore
70 the given signal.
71\end{datadesc}
72
73\begin{datadesc}{SIG*}
74 All the signal numbers are defined symbolically. For example, the
75 hangup signal is defined as \code{signal.SIGHUP}; the variable names
76 are identical to the names used in C programs, as found in
77 \file{signal.h}.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000078 The \UNIX{} man page for \file{signal} lists the existing signals (on
Guido van Rossum626c1e71995-02-07 14:37:02 +000079 some systems this is \file{signal(2)}, on others the list is in
80 \file{signal(7)}).
81 Note that not all systems define the same set of signal names; only
82 those names defined by the system are defined by this module.
83\end{datadesc}
84
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000085\begin{datadesc}{NSIG}
86 One more than the number of the highest signal number.
87\end{datadesc}
88
Guido van Rossum626c1e71995-02-07 14:37:02 +000089The signal module defines the following functions:
90
91\begin{funcdesc}{alarm}{time}
92 If \var{time} is non-zero, this function requests that a
93 \code{SIGALRM} signal be sent to the process in \var{time} seconds.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000094 Any previously scheduled alarm is canceled (i.e.\ only one alarm can
Guido van Rossum626c1e71995-02-07 14:37:02 +000095 be scheduled at any time). The returned value is then the number of
96 seconds before any previously set alarm was to have been delivered.
97 If \var{time} is zero, no alarm id scheduled, and any scheduled
98 alarm is canceled. The return value is the number of seconds
99 remaining before a previously scheduled alarm. If the return value
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000100 is zero, no alarm is currently scheduled. (See the \UNIX{} man page
Guido van Rossum626c1e71995-02-07 14:37:02 +0000101 \code{alarm(2)}.)
102\end{funcdesc}
103
104\begin{funcdesc}{getsignal}{signalnum}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000105 Return the current signal handler for the signal \var{signalnum}.
Guido van Rossum626c1e71995-02-07 14:37:02 +0000106 The returned value may be a callable Python object, or one of the
Guido van Rossume1ff7ad1995-02-15 15:52:32 +0000107 special values \code{signal.SIG_IGN}, \code{signal.SIG_DFL} or
108 \code{None}. Here, \code{signal.SIG_IGN} means that the signal was
109 previously ignored, \code{signal.SIG_DFL} means that the default way
110 of handling the signal was previously in use, and \code{None} means
111 that the previous signal handler was not installed from Python.
Guido van Rossum626c1e71995-02-07 14:37:02 +0000112\end{funcdesc}
113
114\begin{funcdesc}{pause}{}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000115 Cause the process to sleep until a signal is received; the
Guido van Rossum626c1e71995-02-07 14:37:02 +0000116 appropriate handler will then be called. Returns nothing. (See the
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000117 \UNIX{} man page \code{signal(2)}.)
Guido van Rossum626c1e71995-02-07 14:37:02 +0000118\end{funcdesc}
119
120\begin{funcdesc}{signal}{signalnum\, handler}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000121 Set the handler for signal \var{signalnum} to the function
Guido van Rossum626c1e71995-02-07 14:37:02 +0000122 \var{handler}. \var{handler} can be any callable Python object, or
123 one of the special values \code{signal.SIG_IGN} or
Guido van Rossume1ff7ad1995-02-15 15:52:32 +0000124 \code{signal.SIG_DFL}. The previous signal handler will be returned
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000125 (see the description of \code{getsignal()} above). (See the \UNIX{}
Guido van Rossume1ff7ad1995-02-15 15:52:32 +0000126 man page \code{signal(2)}.)
Guido van Rossum626c1e71995-02-07 14:37:02 +0000127
Guido van Rossume1ff7ad1995-02-15 15:52:32 +0000128 When threads are enabled, this function can only be called from the
Guido van Rossum626c1e71995-02-07 14:37:02 +0000129 main thread; attempting to call it from other threads will cause a
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000130 \code{ValueError} exception to be raised.
Guido van Rossum470be141995-03-17 16:07:09 +0000131
132 The \var{handler} is called with two arguments: the signal number
133 and the current stack frame (\code{None} or a frame object; see the
134 reference manual for a description of frame objects).
135\obindex{frame}
Guido van Rossum626c1e71995-02-07 14:37:02 +0000136\end{funcdesc}