Guido van Rossum | 626c1e7 | 1995-02-07 14:37:02 +0000 | [diff] [blame^] | 1 | \section{Built-in Module \sectcode{signal}} |
| 2 | |
| 3 | \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. |
| 15 | |
| 16 | The variables defined in the signal module are: |
| 17 | |
| 18 | \renewcommand{\indexsubitem}{(in module signal)} |
| 19 | \begin{datadesc}{SIG_DFL} |
| 20 | This is one of two standard signal handling options; it will simply |
| 21 | perform the default function for the signal. For example, on most |
| 22 | systems the default action for SIGQUIT is to dump core and exit, |
| 23 | while the default action for SIGCLD is to simply ignore it. |
| 24 | \end{datadesc} |
| 25 | |
| 26 | \begin{datadesc}{SIG_IGN} |
| 27 | This is another standard signal handler, which will simply ignore |
| 28 | the given signal. |
| 29 | \end{datadesc} |
| 30 | |
| 31 | \begin{datadesc}{SIG*} |
| 32 | All the signal numbers are defined symbolically. For example, the |
| 33 | hangup signal is defined as \code{signal.SIGHUP}; the variable names |
| 34 | are identical to the names used in C programs, as found in |
| 35 | \file{signal.h}. |
| 36 | The UNIX man page for \file{signal} lists the existing signals (on |
| 37 | some systems this is \file{signal(2)}, on others the list is in |
| 38 | \file{signal(7)}). |
| 39 | Note that not all systems define the same set of signal names; only |
| 40 | those names defined by the system are defined by this module. |
| 41 | \end{datadesc} |
| 42 | |
| 43 | The signal module defines the following functions: |
| 44 | |
| 45 | \begin{funcdesc}{alarm}{time} |
| 46 | If \var{time} is non-zero, this function requests that a |
| 47 | \code{SIGALRM} signal be sent to the process in \var{time} seconds. |
| 48 | Any previously scheduled alarm is canceled (i.e. only one alarm can |
| 49 | be scheduled at any time). The returned value is then the number of |
| 50 | seconds before any previously set alarm was to have been delivered. |
| 51 | If \var{time} is zero, no alarm id scheduled, and any scheduled |
| 52 | alarm is canceled. The return value is the number of seconds |
| 53 | remaining before a previously scheduled alarm. If the return value |
| 54 | is zero, no alarm is currently scheduled. (See the UNIX man page |
| 55 | \code{alarm(2)}.) |
| 56 | \end{funcdesc} |
| 57 | |
| 58 | \begin{funcdesc}{getsignal}{signalnum} |
| 59 | Returns the current signal handler for the signal \var{signalnum}. |
| 60 | 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}. |
| 62 | \end{funcdesc} |
| 63 | |
| 64 | \begin{funcdesc}{pause}{} |
| 65 | Causes the process to sleep until a signal is received; the |
| 66 | appropriate handler will then be called. Returns nothing. (See the |
| 67 | UNIX man page \code{signal(2)}.) |
| 68 | \end{funcdesc} |
| 69 | |
| 70 | \begin{funcdesc}{signal}{signalnum\, handler} |
| 71 | Sets the handler for signal \var{signalnum} to the function |
| 72 | \var{handler}. \var{handler} can be any callable Python object, or |
| 73 | 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)}.) |
| 76 | |
| 77 | If threads are enabled, this function can only be called from the |
| 78 | main thread; attempting to call it from other threads will cause a |
| 79 | \code{ValueError} exception will be raised. |
| 80 | \end{funcdesc} |