Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`signal` --- Set handlers for asynchronous events |
| 2 | ====================================================== |
| 3 | |
| 4 | .. module:: signal |
| 5 | :synopsis: Set handlers for asynchronous events. |
| 6 | |
| 7 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 8 | This module provides mechanisms to use signal handlers in Python. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 11 | General rules |
| 12 | ------------- |
Jean-Paul Calderone | 6ed7ac4 | 2010-06-19 19:58:37 +0000 | [diff] [blame] | 13 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 14 | The :func:`signal.signal` function allows to define custom handlers to be |
| 15 | executed when a signal is received. A small number of default handlers are |
| 16 | installed: :const:`SIGPIPE` is ignored (so write errors on pipes and sockets |
| 17 | can be reported as ordinary Python exceptions) and :const:`SIGINT` is |
| 18 | translated into a :exc:`KeyboardInterrupt` exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 20 | A handler for a particular signal, once set, remains installed until it is |
| 21 | explicitly reset (Python emulates the BSD style interface regardless of the |
| 22 | underlying implementation), with the exception of the handler for |
| 23 | :const:`SIGCHLD`, which follows the underlying implementation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 25 | There is no way to "block" signals temporarily from critical sections (since |
| 26 | this is not supported by all Unix flavors). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 29 | Execution of Python signal handlers |
| 30 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 31 | |
| 32 | A Python signal handler does not get executed inside the low-level (C) signal |
| 33 | handler. Instead, the low-level signal handler sets a flag which tells the |
| 34 | :term:`virtual machine` to execute the corresponding Python signal handler |
| 35 | at a later point(for example at the next :term:`bytecode` instruction). |
| 36 | This has consequences: |
| 37 | |
| 38 | * It makes little sense to catch synchronous errors like :const:`SIGFPE` or |
| 39 | :const:`SIGSEGV`. |
| 40 | |
| 41 | * A long-running calculation implemented purely in C (such as regular |
| 42 | expression matching on a large body of text) may run uninterrupted for an |
| 43 | arbitrary amount of time, regardless of any signals received. The Python |
| 44 | signal handlers will be called when the calculation finishes. |
| 45 | |
| 46 | |
| 47 | Signals and threads |
| 48 | ^^^^^^^^^^^^^^^^^^^ |
| 49 | |
| 50 | Python signal handlers are always executed in the main Python thread, |
| 51 | even if the signal was received in another thread. This means that signals |
| 52 | can't be used as a means of inter-thread communication. You can use |
| 53 | the synchronization primitives from the :mod:`threading` module instead. |
| 54 | |
| 55 | Besides, only the main thread is allowed to set a new signal handler. |
| 56 | |
| 57 | |
| 58 | Module contents |
| 59 | --------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
| 61 | The variables defined in the :mod:`signal` module are: |
| 62 | |
| 63 | |
| 64 | .. data:: SIG_DFL |
| 65 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 66 | This is one of two standard signal handling options; it will simply perform |
| 67 | the default function for the signal. For example, on most systems the |
| 68 | default action for :const:`SIGQUIT` is to dump core and exit, while the |
| 69 | default action for :const:`SIGCHLD` is to simply ignore it. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | .. data:: SIG_IGN |
| 73 | |
| 74 | This is another standard signal handler, which will simply ignore the given |
| 75 | signal. |
| 76 | |
| 77 | |
| 78 | .. data:: SIG* |
| 79 | |
| 80 | All the signal numbers are defined symbolically. For example, the hangup signal |
| 81 | is defined as :const:`signal.SIGHUP`; the variable names are identical to the |
| 82 | names used in C programs, as found in ``<signal.h>``. The Unix man page for |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 83 | ':c:func:`signal`' lists the existing signals (on some systems this is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | :manpage:`signal(2)`, on others the list is in :manpage:`signal(7)`). Note that |
| 85 | not all systems define the same set of signal names; only those names defined by |
| 86 | the system are defined by this module. |
| 87 | |
| 88 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 89 | .. data:: CTRL_C_EVENT |
| 90 | |
Brian Curtin | f045d77 | 2010-08-05 18:56:00 +0000 | [diff] [blame] | 91 | The signal corresponding to the CTRL+C keystroke event. This signal can |
| 92 | only be used with :func:`os.kill`. |
| 93 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 94 | Availability: Windows. |
| 95 | |
Brian Curtin | 904bd39 | 2010-04-20 15:28:06 +0000 | [diff] [blame] | 96 | .. versionadded:: 3.2 |
| 97 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 98 | |
| 99 | .. data:: CTRL_BREAK_EVENT |
| 100 | |
Brian Curtin | f045d77 | 2010-08-05 18:56:00 +0000 | [diff] [blame] | 101 | The signal corresponding to the CTRL+BREAK keystroke event. This signal can |
| 102 | only be used with :func:`os.kill`. |
| 103 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 104 | Availability: Windows. |
| 105 | |
Brian Curtin | 904bd39 | 2010-04-20 15:28:06 +0000 | [diff] [blame] | 106 | .. versionadded:: 3.2 |
| 107 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 108 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | .. data:: NSIG |
| 110 | |
| 111 | One more than the number of the highest signal number. |
| 112 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 113 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 114 | .. data:: ITIMER_REAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 115 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 116 | Decrements interval timer in real time, and delivers :const:`SIGALRM` upon |
| 117 | expiration. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 118 | |
| 119 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 120 | .. data:: ITIMER_VIRTUAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 121 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 122 | Decrements interval timer only when the process is executing, and delivers |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 123 | SIGVTALRM upon expiration. |
| 124 | |
| 125 | |
| 126 | .. data:: ITIMER_PROF |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 127 | |
| 128 | Decrements interval timer both when the process executes and when the |
| 129 | system is executing on behalf of the process. Coupled with ITIMER_VIRTUAL, |
| 130 | this timer is usually used to profile the time spent by the application |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 131 | in user and kernel space. SIGPROF is delivered upon expiration. |
| 132 | |
| 133 | |
| 134 | The :mod:`signal` module defines one exception: |
| 135 | |
| 136 | .. exception:: ItimerError |
| 137 | |
| 138 | Raised to signal an error from the underlying :func:`setitimer` or |
| 139 | :func:`getitimer` implementation. Expect this error if an invalid |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 140 | interval timer or a negative time is passed to :func:`setitimer`. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 141 | This error is a subtype of :exc:`IOError`. |
| 142 | |
| 143 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | The :mod:`signal` module defines the following functions: |
| 145 | |
| 146 | |
| 147 | .. function:: alarm(time) |
| 148 | |
| 149 | If *time* is non-zero, this function requests that a :const:`SIGALRM` signal be |
| 150 | sent to the process in *time* seconds. Any previously scheduled alarm is |
| 151 | canceled (only one alarm can be scheduled at any time). The returned value is |
| 152 | then the number of seconds before any previously set alarm was to have been |
| 153 | delivered. If *time* is zero, no alarm is scheduled, and any scheduled alarm is |
| 154 | canceled. If the return value is zero, no alarm is currently scheduled. (See |
| 155 | the Unix man page :manpage:`alarm(2)`.) Availability: Unix. |
| 156 | |
| 157 | |
| 158 | .. function:: getsignal(signalnum) |
| 159 | |
| 160 | Return the current signal handler for the signal *signalnum*. The returned value |
| 161 | may be a callable Python object, or one of the special values |
| 162 | :const:`signal.SIG_IGN`, :const:`signal.SIG_DFL` or :const:`None`. Here, |
| 163 | :const:`signal.SIG_IGN` means that the signal was previously ignored, |
| 164 | :const:`signal.SIG_DFL` means that the default way of handling the signal was |
| 165 | previously in use, and ``None`` means that the previous signal handler was not |
| 166 | installed from Python. |
| 167 | |
| 168 | |
| 169 | .. function:: pause() |
| 170 | |
| 171 | Cause the process to sleep until a signal is received; the appropriate handler |
| 172 | will then be called. Returns nothing. Not on Windows. (See the Unix man page |
| 173 | :manpage:`signal(2)`.) |
| 174 | |
| 175 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 176 | .. function:: setitimer(which, seconds[, interval]) |
| 177 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 178 | Sets given interval timer (one of :const:`signal.ITIMER_REAL`, |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 179 | :const:`signal.ITIMER_VIRTUAL` or :const:`signal.ITIMER_PROF`) specified |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 180 | by *which* to fire after *seconds* (float is accepted, different from |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 181 | :func:`alarm`) and after that every *interval* seconds. The interval |
| 182 | timer specified by *which* can be cleared by setting seconds to zero. |
| 183 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 184 | When an interval timer fires, a signal is sent to the process. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 185 | The signal sent is dependent on the timer being used; |
| 186 | :const:`signal.ITIMER_REAL` will deliver :const:`SIGALRM`, |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 187 | :const:`signal.ITIMER_VIRTUAL` sends :const:`SIGVTALRM`, |
| 188 | and :const:`signal.ITIMER_PROF` will deliver :const:`SIGPROF`. |
| 189 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 190 | The old values are returned as a tuple: (delay, interval). |
| 191 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 192 | Attempting to pass an invalid interval timer will cause an |
| 193 | :exc:`ItimerError`. Availability: Unix. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 194 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 195 | |
| 196 | .. function:: getitimer(which) |
| 197 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 198 | Returns current value of a given interval timer specified by *which*. |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 199 | Availability: Unix. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 200 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 201 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 202 | .. function:: set_wakeup_fd(fd) |
| 203 | |
| 204 | Set the wakeup fd to *fd*. When a signal is received, a ``'\0'`` byte is |
| 205 | written to the fd. This can be used by a library to wakeup a poll or select |
| 206 | call, allowing the signal to be fully processed. |
| 207 | |
| 208 | The old wakeup fd is returned. *fd* must be non-blocking. It is up to the |
| 209 | library to remove any bytes before calling poll or select again. |
| 210 | |
| 211 | When threads are enabled, this function can only be called from the main thread; |
| 212 | attempting to call it from other threads will cause a :exc:`ValueError` |
| 213 | exception to be raised. |
| 214 | |
| 215 | |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 216 | .. function:: siginterrupt(signalnum, flag) |
| 217 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 218 | Change system call restart behaviour: if *flag* is :const:`False`, system |
| 219 | calls will be restarted when interrupted by signal *signalnum*, otherwise |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 220 | system calls will be interrupted. Returns nothing. Availability: Unix (see |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 221 | the man page :manpage:`siginterrupt(3)` for further information). |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 222 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 223 | Note that installing a signal handler with :func:`signal` will reset the |
| 224 | restart behaviour to interruptible by implicitly calling |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 225 | :c:func:`siginterrupt` with a true *flag* value for the given signal. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 226 | |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 227 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 228 | .. function:: signal(signalnum, handler) |
| 229 | |
| 230 | Set the handler for signal *signalnum* to the function *handler*. *handler* can |
| 231 | be a callable Python object taking two arguments (see below), or one of the |
| 232 | special values :const:`signal.SIG_IGN` or :const:`signal.SIG_DFL`. The previous |
| 233 | signal handler will be returned (see the description of :func:`getsignal` |
| 234 | above). (See the Unix man page :manpage:`signal(2)`.) |
| 235 | |
| 236 | When threads are enabled, this function can only be called from the main thread; |
| 237 | attempting to call it from other threads will cause a :exc:`ValueError` |
| 238 | exception to be raised. |
| 239 | |
| 240 | The *handler* is called with two arguments: the signal number and the current |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 241 | stack frame (``None`` or a frame object; for a description of frame objects, |
| 242 | see the :ref:`description in the type hierarchy <frame-objects>` or see the |
| 243 | attribute descriptions in the :mod:`inspect` module). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 245 | On Windows, :func:`signal` can only be called with :const:`SIGABRT`, |
| 246 | :const:`SIGFPE`, :const:`SIGILL`, :const:`SIGINT`, :const:`SIGSEGV`, or |
| 247 | :const:`SIGTERM`. A :exc:`ValueError` will be raised in any other case. |
| 248 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | |
| 250 | .. _signal-example: |
| 251 | |
| 252 | Example |
| 253 | ------- |
| 254 | |
| 255 | Here is a minimal example program. It uses the :func:`alarm` function to limit |
| 256 | the time spent waiting to open a file; this is useful if the file is for a |
| 257 | serial device that may not be turned on, which would normally cause the |
| 258 | :func:`os.open` to hang indefinitely. The solution is to set a 5-second alarm |
| 259 | before opening the file; if the operation takes too long, the alarm signal will |
| 260 | be sent, and the handler raises an exception. :: |
| 261 | |
| 262 | import signal, os |
| 263 | |
| 264 | def handler(signum, frame): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 265 | print('Signal handler called with signal', signum) |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 266 | raise IOError("Couldn't open device!") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
| 268 | # Set the signal handler and a 5-second alarm |
| 269 | signal.signal(signal.SIGALRM, handler) |
| 270 | signal.alarm(5) |
| 271 | |
| 272 | # This open() may hang indefinitely |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 273 | fd = os.open('/dev/ttyS0', os.O_RDWR) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 274 | |
| 275 | signal.alarm(0) # Disable the alarm |
| 276 | |