Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{signal}} |
| 2 | |
| 3 | \bimodindex{signal} |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame^] | 4 | This module provides mechanisms to use signal handlers in Python. |
| 5 | Some general rules for working with signals handlers: |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame^] | 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} |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 55 | |
| 56 | The variables defined in the signal module are: |
| 57 | |
| 58 | \renewcommand{\indexsubitem}{(in module signal)} |
| 59 | \begin{datadesc}{SIG_DFL} |
| 60 | This is one of two standard signal handling options; it will simply |
| 61 | perform the default function for the signal. For example, on most |
| 62 | systems the default action for SIGQUIT is to dump core and exit, |
| 63 | while the default action for SIGCLD is to simply ignore it. |
| 64 | \end{datadesc} |
| 65 | |
| 66 | \begin{datadesc}{SIG_IGN} |
| 67 | This is another standard signal handler, which will simply ignore |
| 68 | the given signal. |
| 69 | \end{datadesc} |
| 70 | |
| 71 | \begin{datadesc}{SIG*} |
| 72 | All the signal numbers are defined symbolically. For example, the |
| 73 | hangup signal is defined as \code{signal.SIGHUP}; the variable names |
| 74 | are identical to the names used in C programs, as found in |
| 75 | \file{signal.h}. |
| 76 | The UNIX man page for \file{signal} lists the existing signals (on |
| 77 | some systems this is \file{signal(2)}, on others the list is in |
| 78 | \file{signal(7)}). |
| 79 | Note that not all systems define the same set of signal names; only |
| 80 | those names defined by the system are defined by this module. |
| 81 | \end{datadesc} |
| 82 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame^] | 83 | \begin{datadesc}{NSIG} |
| 84 | One more than the number of the highest signal number. |
| 85 | \end{datadesc} |
| 86 | |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 87 | The signal module defines the following functions: |
| 88 | |
| 89 | \begin{funcdesc}{alarm}{time} |
| 90 | If \var{time} is non-zero, this function requests that a |
| 91 | \code{SIGALRM} signal be sent to the process in \var{time} seconds. |
| 92 | Any previously scheduled alarm is canceled (i.e. only one alarm can |
| 93 | be scheduled at any time). The returned value is then the number of |
| 94 | seconds before any previously set alarm was to have been delivered. |
| 95 | If \var{time} is zero, no alarm id scheduled, and any scheduled |
| 96 | alarm is canceled. The return value is the number of seconds |
| 97 | remaining before a previously scheduled alarm. If the return value |
| 98 | is zero, no alarm is currently scheduled. (See the UNIX man page |
| 99 | \code{alarm(2)}.) |
| 100 | \end{funcdesc} |
| 101 | |
| 102 | \begin{funcdesc}{getsignal}{signalnum} |
| 103 | Returns the current signal handler for the signal \var{signalnum}. |
| 104 | The returned value may be a callable Python object, or one of the |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame^] | 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. |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 110 | \end{funcdesc} |
| 111 | |
| 112 | \begin{funcdesc}{pause}{} |
| 113 | Causes the process to sleep until a signal is received; the |
| 114 | appropriate handler will then be called. Returns nothing. (See the |
| 115 | UNIX man page \code{signal(2)}.) |
| 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{signal}{signalnum\, handler} |
| 119 | Sets the handler for signal \var{signalnum} to the function |
| 120 | \var{handler}. \var{handler} can be any callable Python object, or |
| 121 | one of the special values \code{signal.SIG_IGN} or |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame^] | 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)}.) |
Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | e1ff7ad | 1995-02-15 15:52:32 +0000 | [diff] [blame^] | 126 | 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] | 127 | main thread; attempting to call it from other threads will cause a |
| 128 | \code{ValueError} exception will be raised. |
| 129 | \end{funcdesc} |