blob: 86d69676e5fb5732e8858ec4c5180cd25ed85c3c [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{signal}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{signal}
Guido van Rossum626c1e71995-02-07 14:37:02 +00003
Fred Drakeb91e9341998-07-23 17:59:49 +00004
5\modulesynopsis{Set handlers for asynchronous events.}
6
Guido van Rossume1ff7ad1995-02-15 15:52:32 +00007This module provides mechanisms to use signal handlers in Python.
8Some general rules for working with signals handlers:
Guido van Rossum626c1e71995-02-07 14:37:02 +00009
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000010\begin{itemize}
11
12\item
13A handler for a particular signal, once set, remains installed until
Guido van Rossumc1715521996-02-12 23:18:51 +000014it is explicitly reset (i.e. Python emulates the BSD style interface
15regardless of the underlying implementation), with the exception of
Fred Drake55f44921998-01-22 15:56:41 +000016the handler for \constant{SIGCHLD}, which follows the underlying
Guido van Rossumc1715521996-02-12 23:18:51 +000017implementation.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000018
19\item
20There is no way to ``block'' signals temporarily from critical
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000021sections (since this is not supported by all \UNIX{} flavors).
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000022
23\item
24Although Python signal handlers are called asynchronously as far as
25the Python user is concerned, they can only occur between the
26``atomic'' instructions of the Python interpreter. This means that
Fred Drake55f44921998-01-22 15:56:41 +000027signals arriving during long calculations implemented purely in \C{}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000028(e.g.\ regular expression matches on large bodies of text) may be
Guido van Rossum470be141995-03-17 16:07:09 +000029delayed for an arbitrary amount of time.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000030
31\item
32When a signal arrives during an I/O operation, it is possible that the
33I/O operation raises an exception after the signal handler returns.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000034This is dependent on the underlying \UNIX{} system's semantics regarding
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000035interrupted system calls.
36
37\item
Fred Drake55f44921998-01-22 15:56:41 +000038Because the \C{} signal handler always returns, it makes little sense to
39catch synchronous errors like \constant{SIGFPE} or \constant{SIGSEGV}.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000040
41\item
42Python installs a small number of signal handlers by default:
Fred Drake55f44921998-01-22 15:56:41 +000043\constant{SIGPIPE} is ignored (so write errors on pipes and sockets can be
44reported as ordinary Python exceptions), \constant{SIGINT} is translated
45into a \exception{KeyboardInterrupt} exception, and \constant{SIGTERM} is
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000046caught so that necessary cleanup (especially \code{sys.exitfunc}) can
47be performed before actually terminating. All of these can be
48overridden.
49
50\item
51Some care must be taken if both signals and threads are used in the
52same program. The fundamental thing to remember in using signals and
Fred Drake55f44921998-01-22 15:56:41 +000053threads simultaneously is:\ always perform \function{signal()} operations
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000054in the main thread of execution. Any thread can perform an
Fred Drake55f44921998-01-22 15:56:41 +000055\function{alarm()}, \function{getsignal()}, or \function{pause()};
56only the main thread can set a new signal handler, and the main thread
57will be the only one to receive signals (this is enforced by the
58Python \module{signal} module, even if the underlying thread
59implementation supports sending signals to individual threads). This
60means that signals can't be used as a means of interthread
61communication. Use locks instead.
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000062
63\end{itemize}
Guido van Rossum626c1e71995-02-07 14:37:02 +000064
Fred Drake55f44921998-01-22 15:56:41 +000065The variables defined in the \module{signal} module are:
Guido van Rossum626c1e71995-02-07 14:37:02 +000066
Guido van Rossum626c1e71995-02-07 14:37:02 +000067\begin{datadesc}{SIG_DFL}
68 This is one of two standard signal handling options; it will simply
69 perform the default function for the signal. For example, on most
Fred Drake55f44921998-01-22 15:56:41 +000070 systems the default action for \constant{SIGQUIT} is to dump core
71 and exit, while the default action for \constant{SIGCLD} is to
72 simply ignore it.
Guido van Rossum626c1e71995-02-07 14:37:02 +000073\end{datadesc}
74
75\begin{datadesc}{SIG_IGN}
76 This is another standard signal handler, which will simply ignore
77 the given signal.
78\end{datadesc}
79
80\begin{datadesc}{SIG*}
81 All the signal numbers are defined symbolically. For example, the
Fred Drake55f44921998-01-22 15:56:41 +000082 hangup signal is defined as \constant{signal.SIGHUP}; the variable names
Guido van Rossum626c1e71995-02-07 14:37:02 +000083 are identical to the names used in C programs, as found in
Fred Drake55f44921998-01-22 15:56:41 +000084 \file{<signal.h>}.
85 The \UNIX{} man page for `\cfunction{signal()}' lists the existing
86 signals (on some systems this is \manpage{signal}{2}, on others the
87 list is in \manpage{signal}{7}).
Guido van Rossum626c1e71995-02-07 14:37:02 +000088 Note that not all systems define the same set of signal names; only
89 those names defined by the system are defined by this module.
90\end{datadesc}
91
Guido van Rossume1ff7ad1995-02-15 15:52:32 +000092\begin{datadesc}{NSIG}
93 One more than the number of the highest signal number.
94\end{datadesc}
95
Fred Drake55f44921998-01-22 15:56:41 +000096The \module{signal} module defines the following functions:
Guido van Rossum626c1e71995-02-07 14:37:02 +000097
98\begin{funcdesc}{alarm}{time}
99 If \var{time} is non-zero, this function requests that a
Fred Drake55f44921998-01-22 15:56:41 +0000100 \constant{SIGALRM} signal be sent to the process in \var{time} seconds.
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000101 Any previously scheduled alarm is canceled (i.e.\ only one alarm can
Guido van Rossum626c1e71995-02-07 14:37:02 +0000102 be scheduled at any time). The returned value is then the number of
103 seconds before any previously set alarm was to have been delivered.
104 If \var{time} is zero, no alarm id scheduled, and any scheduled
105 alarm is canceled. The return value is the number of seconds
106 remaining before a previously scheduled alarm. If the return value
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000107 is zero, no alarm is currently scheduled. (See the \UNIX{} man page
Fred Drake55f44921998-01-22 15:56:41 +0000108 \manpage{alarm}{2}.)
Guido van Rossum626c1e71995-02-07 14:37:02 +0000109\end{funcdesc}
110
111\begin{funcdesc}{getsignal}{signalnum}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000112 Return the current signal handler for the signal \var{signalnum}.
Guido van Rossum626c1e71995-02-07 14:37:02 +0000113 The returned value may be a callable Python object, or one of the
Fred Drake55f44921998-01-22 15:56:41 +0000114 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 Rossum626c1e71995-02-07 14:37:02 +0000120\end{funcdesc}
121
122\begin{funcdesc}{pause}{}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000123 Cause the process to sleep until a signal is received; the
Guido van Rossum626c1e71995-02-07 14:37:02 +0000124 appropriate handler will then be called. Returns nothing. (See the
Fred Drake55f44921998-01-22 15:56:41 +0000125 \UNIX{} man page \manpage{signal}{2}.)
Guido van Rossum626c1e71995-02-07 14:37:02 +0000126\end{funcdesc}
127
Fred Drakecce10901998-03-17 06:33:25 +0000128\begin{funcdesc}{signal}{signalnum, handler}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000129 Set the handler for signal \var{signalnum} to the function
Guido van Rossum52481481998-06-09 15:42:25 +0000130 \var{handler}. \var{handler} can be a callable Python object
131 taking two arguments (see below), or
Fred Drake55f44921998-01-22 15:56:41 +0000132 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 Rossum626c1e71995-02-07 14:37:02 +0000136
Guido van Rossume1ff7ad1995-02-15 15:52:32 +0000137 When threads are enabled, this function can only be called from the
Guido van Rossum626c1e71995-02-07 14:37:02 +0000138 main thread; attempting to call it from other threads will cause a
Fred Drake55f44921998-01-22 15:56:41 +0000139 \exception{ValueError} exception to be raised.
Guido van Rossum470be141995-03-17 16:07:09 +0000140
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 Rossum626c1e71995-02-07 14:37:02 +0000145\end{funcdesc}