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