11\section {Built-in Module \sectcode {signal} }
22
33\bimodindex {signal}
4- This module provides mechanisms to write signal handlers in Python.
5-
6- {\bf Warning:} Some care must be taken if both signals and threads
7- will be used in the same program. The fundamental thing to remember
8- in using signals and threads simultaneously is: always perform
9- \code {signal()} operations in the main thread of execution. Any
10- thread can perform a \code {alarm()}, \code {getsignal()}, or
11- \code {pause()}; only the main thread can set a new signal handler, and
12- the main thread will be the only one to receive signals. This means
13- that signals can't be used as a means of interthread communication.
14- Use locks instead.
4+ This module provides mechanisms to use signal handlers in Python.
5+ Some general rules for working with signals handlers:
6+
7+ \begin {itemize }
8+
9+ \item
10+ A handler for a particular signal, once set, remains installed until
11+ it is explicitly reset (i.e. Python uses the BSD style interface).
12+
13+ \item
14+ There is no way to `` block'' signals temporarily from critical
15+ sections (since this is not supported by all Unix flavors).
16+
17+ \item
18+ Although Python signal handlers are called asynchronously as far as
19+ the Python user is concerned, they can only occur between the
20+ `` atomic'' instructions of the Python interpreter. This means that
21+ signals arriving during long calculations implemented purely in C
22+ (e.g. regular expression matches on large bodies of text) may be
23+ delayed for an arbitrary time.
24+
25+ \item
26+ When a signal arrives during an I/O operation, it is possible that the
27+ I/O operation raises an exception after the signal handler returns.
28+ This is dependent on the underlying Unix system's semantics regarding
29+ interrupted system calls.
30+
31+ \item
32+ Because the C signal handler always returns, it makes little sense to
33+ catch synchronous errors like \code {SIGFPE} or \code {SIGSEGV}.
34+
35+ \item
36+ Python installs a small number of signal handlers by default:
37+ \code {SIGPIPE} is ignored (so write errors on pipes and sockets can be
38+ reported as ordinary Python exceptions), \code {SIGINT} is translated
39+ into a \code {KeyboardInterrupt} exception, and \code {SIGTERM} is
40+ caught so that necessary cleanup (especially \code {sys.exitfunc}) can
41+ be performed before actually terminating. All of these can be
42+ overridden.
43+
44+ \item
45+ Some care must be taken if both signals and threads are used in the
46+ same program. The fundamental thing to remember in using signals and
47+ threads simultaneously is: always perform \code {signal()} operations
48+ in the main thread of execution. Any thread can perform a
49+ \code {alarm()}, \code {getsignal()}, or \code {pause()}; only the main
50+ thread can set a new signal handler, and the main thread will be the
51+ only one to receive signals. This means that signals can't be used as
52+ a means of interthread communication. Use locks instead.
53+
54+ \end {itemize }
1555
1656The variables defined in the signal module are:
1757
@@ -40,6 +80,10 @@ \section{Built-in Module \sectcode{signal}}
4080 those names defined by the system are defined by this module.
4181\end {datadesc }
4282
83+ \begin {datadesc }{NSIG}
84+ One more than the number of the highest signal number.
85+ \end {datadesc }
86+
4387The signal module defines the following functions:
4488
4589\begin {funcdesc }{alarm}{time}
@@ -58,7 +102,11 @@ \section{Built-in Module \sectcode{signal}}
58102\begin {funcdesc }{getsignal}{signalnum}
59103 Returns the current signal handler for the signal \var {signalnum}.
60104 The returned value may be a callable Python object, or one of the
61- special values \code {signal.SIG_IGN} or \code {signal.SIG_DFL}.
105+ special values \code {signal.SIG_IGN}, \code {signal.SIG_DFL} or
106+ \code {None}. Here, \code {signal.SIG_IGN} means that the signal was
107+ previously ignored, \code {signal.SIG_DFL} means that the default way
108+ of handling the signal was previously in use, and \code {None} means
109+ that the previous signal handler was not installed from Python.
62110\end {funcdesc }
63111
64112\begin {funcdesc }{pause}{}
@@ -71,10 +119,11 @@ \section{Built-in Module \sectcode{signal}}
71119 Sets the handler for signal \var {signalnum} to the function
72120 \var {handler}. \var {handler} can be any callable Python object, or
73121 one of the special values \code {signal.SIG_IGN} or
74- \code {signal.SIG_DFL}. The previous signal handler will be
75- returned. (See the UNIX man page \code {signal(2)}.)
122+ \code {signal.SIG_DFL}. The previous signal handler will be returned
123+ (see the description of \code {getsignal()} above). (See the UNIX
124+ man page \code {signal(2)}.)
76125
77- If threads are enabled, this function can only be called from the
126+ When threads are enabled, this function can only be called from the
78127 main thread; attempting to call it from other threads will cause a
79128 \code {ValueError} exception will be raised.
80129\end {funcdesc }
0 commit comments