Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{signal}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-signal} |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 3 | |
| 4 | \bimodindex{signal} |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 5 | This module provides mechanisms to use signal handlers in Python. |
| 6 | Some general rules for working with signals handlers: |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 8 | \begin{itemize} |
| 9 | |
| 10 | \item |
| 11 | A handler for a particular signal, once set, remains installed until |
Guido van Rossum | c171552 | 1996-02-12 23:18:51 +0000 | [diff] [blame] | 12 | it is explicitly reset (i.e. Python emulates the BSD style interface |
| 13 | regardless of the underlying implementation), with the exception of |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 14 | the handler for \constant{SIGCHLD}, which follows the underlying |
Guido van Rossum | c171552 | 1996-02-12 23:18:51 +0000 | [diff] [blame] | 15 | implementation. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 16 | |
| 17 | \item |
| 18 | There is no way to ``block'' signals temporarily from critical |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 19 | sections (since this is not supported by all \UNIX{} flavors). |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 20 | |
| 21 | \item |
| 22 | Although Python signal handlers are called asynchronously as far as |
| 23 | the Python user is concerned, they can only occur between the |
| 24 | ``atomic'' instructions of the Python interpreter. This means that |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 25 | signals arriving during long calculations implemented purely in \C{} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 26 | (e.g.\ regular expression matches on large bodies of text) may be |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 27 | delayed for an arbitrary amount of time. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 28 | |
| 29 | \item |
| 30 | When a signal arrives during an I/O operation, it is possible that the |
| 31 | I/O operation raises an exception after the signal handler returns. |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 32 | This is dependent on the underlying \UNIX{} system's semantics regarding |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 33 | interrupted system calls. |
| 34 | |
| 35 | \item |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 36 | Because the \C{} signal handler always returns, it makes little sense to |
| 37 | catch synchronous errors like \constant{SIGFPE} or \constant{SIGSEGV}. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 38 | |
| 39 | \item |
| 40 | Python installs a small number of signal handlers by default: |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 41 | \constant{SIGPIPE} is ignored (so write errors on pipes and sockets can be |
| 42 | reported as ordinary Python exceptions), \constant{SIGINT} is translated |
| 43 | into a \exception{KeyboardInterrupt} exception, and \constant{SIGTERM} is |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 44 | caught so that necessary cleanup (especially \code{sys.exitfunc}) can |
| 45 | be performed before actually terminating. All of these can be |
| 46 | overridden. |
| 47 | |
| 48 | \item |
| 49 | Some care must be taken if both signals and threads are used in the |
| 50 | same program. The fundamental thing to remember in using signals and |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 51 | threads simultaneously is:\ always perform \function{signal()} operations |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 52 | in the main thread of execution. Any thread can perform an |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 53 | \function{alarm()}, \function{getsignal()}, or \function{pause()}; |
| 54 | only the main thread can set a new signal handler, and the main thread |
| 55 | will be the only one to receive signals (this is enforced by the |
| 56 | Python \module{signal} module, even if the underlying thread |
| 57 | implementation supports sending signals to individual threads). This |
| 58 | means that signals can't be used as a means of interthread |
| 59 | communication. Use locks instead. |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 60 | |
| 61 | \end{itemize} |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 62 | |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 63 | The variables defined in the \module{signal} module are: |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 64 | |
| 65 | \renewcommand{\indexsubitem}{(in module signal)} |
| 66 | \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 Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 69 | 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 Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 72 | \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 Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 81 | hangup signal is defined as \constant{signal.SIGHUP}; the variable names |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 82 | are identical to the names used in C programs, as found in |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 83 | \file{<signal.h>}. |
| 84 | 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 Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 87 | 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 Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 91 | \begin{datadesc}{NSIG} |
| 92 | One more than the number of the highest signal number. |
| 93 | \end{datadesc} |
| 94 | |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 95 | The \module{signal} module defines the following functions: |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 96 | |
| 97 | \begin{funcdesc}{alarm}{time} |
| 98 | If \var{time} is non-zero, this function requests that a |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 99 | \constant{SIGALRM} signal be sent to the process in \var{time} seconds. |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 100 | Any previously scheduled alarm is canceled (i.e.\ only one alarm can |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 101 | 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 Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 106 | is zero, no alarm is currently scheduled. (See the \UNIX{} man page |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 107 | \manpage{alarm}{2}.) |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 108 | \end{funcdesc} |
| 109 | |
| 110 | \begin{funcdesc}{getsignal}{signalnum} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 111 | Return the current signal handler for the signal \var{signalnum}. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 112 | The returned value may be a callable Python object, or one of the |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 113 | 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 Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 119 | \end{funcdesc} |
| 120 | |
| 121 | \begin{funcdesc}{pause}{} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 122 | Cause the process to sleep until a signal is received; the |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 123 | appropriate handler will then be called. Returns nothing. (See the |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 124 | \UNIX{} man page \manpage{signal}{2}.) |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 125 | \end{funcdesc} |
| 126 | |
| 127 | \begin{funcdesc}{signal}{signalnum\, handler} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 128 | Set the handler for signal \var{signalnum} to the function |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 129 | \var{handler}. \var{handler} can be any callable Python object, or |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 130 | one of the special values \constant{signal.SIG_IGN} or |
| 131 | \constant{signal.SIG_DFL}. The previous signal handler will be returned |
| 132 | (see the description of \function{getsignal()} above). (See the |
| 133 | \UNIX{} man page \manpage{signal}{2}.) |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame] | 135 | When threads are enabled, this function can only be called from the |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 136 | main thread; attempting to call it from other threads will cause a |
Fred Drake | 55f4492 | 1998-01-22 15:56:41 +0000 | [diff] [blame^] | 137 | \exception{ValueError} exception to be raised. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 138 | |
| 139 | The \var{handler} is called with two arguments: the signal number |
| 140 | and the current stack frame (\code{None} or a frame object; see the |
| 141 | reference manual for a description of frame objects). |
| 142 | \obindex{frame} |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 143 | \end{funcdesc} |