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 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 9 | This module provides mechanisms to use signal handlers in Python. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 12 | General rules |
| 13 | ------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 15 | The :func:`signal.signal` function allows defining custom handlers to be |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 16 | executed when a signal is received. A small number of default handlers are |
| 17 | installed: :const:`SIGPIPE` is ignored (so write errors on pipes and sockets |
| 18 | can be reported as ordinary Python exceptions) and :const:`SIGINT` is |
| 19 | translated into a :exc:`KeyboardInterrupt` exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 21 | A handler for a particular signal, once set, remains installed until it is |
| 22 | explicitly reset (Python emulates the BSD style interface regardless of the |
| 23 | underlying implementation), with the exception of the handler for |
| 24 | :const:`SIGCHLD`, which follows the underlying implementation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 27 | Execution of Python signal handlers |
| 28 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 29 | |
| 30 | A Python signal handler does not get executed inside the low-level (C) signal |
| 31 | handler. Instead, the low-level signal handler sets a flag which tells the |
| 32 | :term:`virtual machine` to execute the corresponding Python signal handler |
| 33 | at a later point(for example at the next :term:`bytecode` instruction). |
| 34 | This has consequences: |
| 35 | |
| 36 | * It makes little sense to catch synchronous errors like :const:`SIGFPE` or |
Georg Brandl | c377fe2 | 2013-10-06 21:22:42 +0200 | [diff] [blame] | 37 | :const:`SIGSEGV` that are caused by an invalid operation in C code. Python |
| 38 | will return from the signal handler to the C code, which is likely to raise |
| 39 | the same signal again, causing Python to apparently hang. From Python 3.3 |
| 40 | onwards, you can use the :mod:`faulthandler` module to report on synchronous |
| 41 | errors. |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 42 | |
| 43 | * A long-running calculation implemented purely in C (such as regular |
| 44 | expression matching on a large body of text) may run uninterrupted for an |
| 45 | arbitrary amount of time, regardless of any signals received. The Python |
| 46 | signal handlers will be called when the calculation finishes. |
| 47 | |
| 48 | |
Antoine Pitrou | 682d443 | 2012-03-31 21:09:00 +0200 | [diff] [blame] | 49 | .. _signals-and-threads: |
| 50 | |
| 51 | |
Antoine Pitrou | 6afd11c | 2012-03-31 20:56:21 +0200 | [diff] [blame] | 52 | Signals and threads |
| 53 | ^^^^^^^^^^^^^^^^^^^ |
| 54 | |
| 55 | Python signal handlers are always executed in the main Python thread, |
| 56 | even if the signal was received in another thread. This means that signals |
| 57 | can't be used as a means of inter-thread communication. You can use |
| 58 | the synchronization primitives from the :mod:`threading` module instead. |
| 59 | |
| 60 | Besides, only the main thread is allowed to set a new signal handler. |
| 61 | |
| 62 | |
| 63 | Module contents |
| 64 | --------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 66 | .. versionchanged:: 3.5 |
| 67 | signal (SIG*), handler (:const:`SIG_DFL`, :const:`SIG_IGN`) and sigmask |
| 68 | (:const:`SIG_BLOCK`, :const:`SIG_UNBLOCK`, :const:`SIG_SETMASK`) |
| 69 | related constants listed below were turned into |
| 70 | :class:`enums <enum.IntEnum>`. |
| 71 | :func:`getsignal`, :func:`pthread_sigmask`, :func:`sigpending` and |
| 72 | :func:`sigwait` functions return human-readable |
| 73 | :class:`enums <enum.IntEnum>`. |
| 74 | |
| 75 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | The variables defined in the :mod:`signal` module are: |
| 77 | |
| 78 | |
| 79 | .. data:: SIG_DFL |
| 80 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 81 | This is one of two standard signal handling options; it will simply perform |
| 82 | the default function for the signal. For example, on most systems the |
| 83 | default action for :const:`SIGQUIT` is to dump core and exit, while the |
| 84 | default action for :const:`SIGCHLD` is to simply ignore it. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | .. data:: SIG_IGN |
| 88 | |
| 89 | This is another standard signal handler, which will simply ignore the given |
| 90 | signal. |
| 91 | |
| 92 | |
| 93 | .. data:: SIG* |
| 94 | |
| 95 | All the signal numbers are defined symbolically. For example, the hangup signal |
| 96 | is defined as :const:`signal.SIGHUP`; the variable names are identical to the |
| 97 | 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] | 98 | ':c:func:`signal`' lists the existing signals (on some systems this is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | :manpage:`signal(2)`, on others the list is in :manpage:`signal(7)`). Note that |
| 100 | not all systems define the same set of signal names; only those names defined by |
| 101 | the system are defined by this module. |
| 102 | |
| 103 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 104 | .. data:: CTRL_C_EVENT |
| 105 | |
Serhiy Storchaka | 0424eaf | 2015-09-12 17:45:25 +0300 | [diff] [blame] | 106 | The signal corresponding to the :kbd:`Ctrl+C` keystroke event. This signal can |
Brian Curtin | f045d77 | 2010-08-05 18:56:00 +0000 | [diff] [blame] | 107 | only be used with :func:`os.kill`. |
| 108 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 109 | .. availability:: Windows. |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 110 | |
Brian Curtin | 904bd39 | 2010-04-20 15:28:06 +0000 | [diff] [blame] | 111 | .. versionadded:: 3.2 |
| 112 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 113 | |
| 114 | .. data:: CTRL_BREAK_EVENT |
| 115 | |
Serhiy Storchaka | 0424eaf | 2015-09-12 17:45:25 +0300 | [diff] [blame] | 116 | The signal corresponding to the :kbd:`Ctrl+Break` keystroke event. This signal can |
Brian Curtin | f045d77 | 2010-08-05 18:56:00 +0000 | [diff] [blame] | 117 | only be used with :func:`os.kill`. |
| 118 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 119 | .. availability:: Windows. |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 120 | |
Brian Curtin | 904bd39 | 2010-04-20 15:28:06 +0000 | [diff] [blame] | 121 | .. versionadded:: 3.2 |
| 122 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 123 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | .. data:: NSIG |
| 125 | |
| 126 | One more than the number of the highest signal number. |
| 127 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 128 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 129 | .. data:: ITIMER_REAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 130 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 131 | Decrements interval timer in real time, and delivers :const:`SIGALRM` upon |
| 132 | expiration. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 133 | |
| 134 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 135 | .. data:: ITIMER_VIRTUAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 136 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 137 | 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] | 138 | SIGVTALRM upon expiration. |
| 139 | |
| 140 | |
| 141 | .. data:: ITIMER_PROF |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 142 | |
| 143 | Decrements interval timer both when the process executes and when the |
| 144 | system is executing on behalf of the process. Coupled with ITIMER_VIRTUAL, |
| 145 | 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] | 146 | in user and kernel space. SIGPROF is delivered upon expiration. |
| 147 | |
| 148 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 149 | .. data:: SIG_BLOCK |
| 150 | |
| 151 | A possible value for the *how* parameter to :func:`pthread_sigmask` |
| 152 | indicating that signals are to be blocked. |
| 153 | |
| 154 | .. versionadded:: 3.3 |
| 155 | |
| 156 | .. data:: SIG_UNBLOCK |
| 157 | |
| 158 | A possible value for the *how* parameter to :func:`pthread_sigmask` |
| 159 | indicating that signals are to be unblocked. |
| 160 | |
| 161 | .. versionadded:: 3.3 |
| 162 | |
| 163 | .. data:: SIG_SETMASK |
| 164 | |
| 165 | A possible value for the *how* parameter to :func:`pthread_sigmask` |
| 166 | indicating that the signal mask is to be replaced. |
| 167 | |
| 168 | .. versionadded:: 3.3 |
| 169 | |
| 170 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 171 | The :mod:`signal` module defines one exception: |
| 172 | |
| 173 | .. exception:: ItimerError |
| 174 | |
| 175 | Raised to signal an error from the underlying :func:`setitimer` or |
| 176 | :func:`getitimer` implementation. Expect this error if an invalid |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 177 | interval timer or a negative time is passed to :func:`setitimer`. |
Antoine Pitrou | 4272d6a | 2011-10-12 19:10:10 +0200 | [diff] [blame] | 178 | This error is a subtype of :exc:`OSError`. |
| 179 | |
| 180 | .. versionadded:: 3.3 |
| 181 | This error used to be a subtype of :exc:`IOError`, which is now an |
| 182 | alias of :exc:`OSError`. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 183 | |
| 184 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 185 | The :mod:`signal` module defines the following functions: |
| 186 | |
| 187 | |
| 188 | .. function:: alarm(time) |
| 189 | |
| 190 | If *time* is non-zero, this function requests that a :const:`SIGALRM` signal be |
| 191 | sent to the process in *time* seconds. Any previously scheduled alarm is |
| 192 | canceled (only one alarm can be scheduled at any time). The returned value is |
| 193 | then the number of seconds before any previously set alarm was to have been |
| 194 | delivered. If *time* is zero, no alarm is scheduled, and any scheduled alarm is |
| 195 | canceled. If the return value is zero, no alarm is currently scheduled. (See |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 196 | the Unix man page :manpage:`alarm(2)`.) |
| 197 | |
| 198 | .. availability:: Unix. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | .. function:: getsignal(signalnum) |
| 202 | |
| 203 | Return the current signal handler for the signal *signalnum*. The returned value |
| 204 | may be a callable Python object, or one of the special values |
| 205 | :const:`signal.SIG_IGN`, :const:`signal.SIG_DFL` or :const:`None`. Here, |
| 206 | :const:`signal.SIG_IGN` means that the signal was previously ignored, |
| 207 | :const:`signal.SIG_DFL` means that the default way of handling the signal was |
| 208 | previously in use, and ``None`` means that the previous signal handler was not |
| 209 | installed from Python. |
| 210 | |
| 211 | |
Antoine Pietri | 5d2a27d | 2018-03-12 14:42:34 +0100 | [diff] [blame] | 212 | .. function:: strsignal(signalnum) |
| 213 | |
| 214 | Return the system description of the signal *signalnum*, such as |
| 215 | "Interrupt", "Segmentation fault", etc. Returns :const:`None` if the signal |
| 216 | is not recognized. |
| 217 | |
| 218 | .. versionadded:: 3.8 |
| 219 | |
| 220 | |
Antoine Pitrou | 9d3627e | 2018-05-04 13:00:50 +0200 | [diff] [blame] | 221 | .. function:: valid_signals() |
| 222 | |
| 223 | Return the set of valid signal numbers on this platform. This can be |
| 224 | less than ``range(1, NSIG)`` if some signals are reserved by the system |
| 225 | for internal use. |
| 226 | |
| 227 | .. versionadded:: 3.8 |
| 228 | |
| 229 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | .. function:: pause() |
| 231 | |
| 232 | Cause the process to sleep until a signal is received; the appropriate handler |
| 233 | will then be called. Returns nothing. Not on Windows. (See the Unix man page |
| 234 | :manpage:`signal(2)`.) |
| 235 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 236 | See also :func:`sigwait`, :func:`sigwaitinfo`, :func:`sigtimedwait` and |
| 237 | :func:`sigpending`. |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 238 | |
| 239 | |
Vladimir Matveev | c24c6c2 | 2019-01-08 01:58:25 -0800 | [diff] [blame^] | 240 | .. function:: raise_signal(signum) |
| 241 | |
| 242 | Sends a signal to the calling process. Returns nothing. |
| 243 | |
| 244 | .. versionadded:: 3.8 |
| 245 | |
| 246 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 247 | .. function:: pthread_kill(thread_id, signalnum) |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 248 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 249 | Send the signal *signalnum* to the thread *thread_id*, another thread in the |
Antoine Pitrou | 682d443 | 2012-03-31 21:09:00 +0200 | [diff] [blame] | 250 | same process as the caller. The target thread can be executing any code |
| 251 | (Python or not). However, if the target thread is executing the Python |
| 252 | interpreter, the Python signal handlers will be :ref:`executed by the main |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 253 | thread <signals-and-threads>`. Therefore, the only point of sending a |
| 254 | signal to a particular Python thread would be to force a running system call |
| 255 | to fail with :exc:`InterruptedError`. |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 256 | |
Victor Stinner | 2a12974 | 2011-05-30 23:02:52 +0200 | [diff] [blame] | 257 | Use :func:`threading.get_ident()` or the :attr:`~threading.Thread.ident` |
Antoine Pitrou | 682d443 | 2012-03-31 21:09:00 +0200 | [diff] [blame] | 258 | attribute of :class:`threading.Thread` objects to get a suitable value |
| 259 | for *thread_id*. |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 260 | |
Tal Einat | c7027b7 | 2015-05-16 14:14:49 +0300 | [diff] [blame] | 261 | If *signalnum* is 0, then no signal is sent, but error checking is still |
Antoine Pitrou | 682d443 | 2012-03-31 21:09:00 +0200 | [diff] [blame] | 262 | performed; this can be used to check if the target thread is still running. |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 263 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 264 | .. availability:: Unix (see the man page :manpage:`pthread_kill(3)` for further |
| 265 | information). |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 266 | |
| 267 | See also :func:`os.kill`. |
| 268 | |
| 269 | .. versionadded:: 3.3 |
| 270 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 271 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 272 | .. function:: pthread_sigmask(how, mask) |
| 273 | |
| 274 | Fetch and/or change the signal mask of the calling thread. The signal mask |
| 275 | is the set of signals whose delivery is currently blocked for the caller. |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 276 | Return the old signal mask as a set of signals. |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 277 | |
| 278 | The behavior of the call is dependent on the value of *how*, as follows. |
| 279 | |
Antoine Pitrou | 8bbe9b4 | 2012-03-31 21:09:53 +0200 | [diff] [blame] | 280 | * :data:`SIG_BLOCK`: The set of blocked signals is the union of the current |
| 281 | set and the *mask* argument. |
| 282 | * :data:`SIG_UNBLOCK`: The signals in *mask* are removed from the current |
| 283 | set of blocked signals. It is permissible to attempt to unblock a |
| 284 | signal which is not blocked. |
| 285 | * :data:`SIG_SETMASK`: The set of blocked signals is set to the *mask* |
| 286 | argument. |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 287 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 288 | *mask* is a set of signal numbers (e.g. {:const:`signal.SIGINT`, |
Antoine Pitrou | 9d3627e | 2018-05-04 13:00:50 +0200 | [diff] [blame] | 289 | :const:`signal.SIGTERM`}). Use :func:`~signal.valid_signals` for a full |
| 290 | mask including all signals. |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 291 | |
| 292 | For example, ``signal.pthread_sigmask(signal.SIG_BLOCK, [])`` reads the |
| 293 | signal mask of the calling thread. |
| 294 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 295 | .. availability:: Unix. See the man page :manpage:`sigprocmask(3)` and |
| 296 | :manpage:`pthread_sigmask(3)` for further information. |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 297 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 298 | See also :func:`pause`, :func:`sigpending` and :func:`sigwait`. |
| 299 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 300 | .. versionadded:: 3.3 |
| 301 | |
| 302 | |
Victor Stinner | ef611c9 | 2017-10-13 13:49:43 -0700 | [diff] [blame] | 303 | .. function:: setitimer(which, seconds, interval=0.0) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 304 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 305 | Sets given interval timer (one of :const:`signal.ITIMER_REAL`, |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 306 | :const:`signal.ITIMER_VIRTUAL` or :const:`signal.ITIMER_PROF`) specified |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 307 | by *which* to fire after *seconds* (float is accepted, different from |
Victor Stinner | ef611c9 | 2017-10-13 13:49:43 -0700 | [diff] [blame] | 308 | :func:`alarm`) and after that every *interval* seconds (if *interval* |
| 309 | is non-zero). The interval timer specified by *which* can be cleared by |
| 310 | setting *seconds* to zero. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 311 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 312 | When an interval timer fires, a signal is sent to the process. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 313 | The signal sent is dependent on the timer being used; |
| 314 | :const:`signal.ITIMER_REAL` will deliver :const:`SIGALRM`, |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 315 | :const:`signal.ITIMER_VIRTUAL` sends :const:`SIGVTALRM`, |
| 316 | and :const:`signal.ITIMER_PROF` will deliver :const:`SIGPROF`. |
| 317 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 318 | The old values are returned as a tuple: (delay, interval). |
| 319 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 320 | Attempting to pass an invalid interval timer will cause an |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 321 | :exc:`ItimerError`. |
| 322 | |
| 323 | .. availability:: Unix. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 324 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 325 | |
| 326 | .. function:: getitimer(which) |
| 327 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 328 | Returns current value of a given interval timer specified by *which*. |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 329 | |
| 330 | .. availability:: Unix. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 331 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 332 | |
Nathaniel J. Smith | 902ab80 | 2017-12-17 20:10:18 -0800 | [diff] [blame] | 333 | .. function:: set_wakeup_fd(fd, *, warn_on_full_buffer=True) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 334 | |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 335 | Set the wakeup file descriptor to *fd*. When a signal is received, the |
| 336 | signal number is written as a single byte into the fd. This can be used by |
| 337 | a library to wakeup a poll or select call, allowing the signal to be fully |
| 338 | processed. |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | d79c1d4 | 2017-06-13 10:14:09 +0200 | [diff] [blame] | 340 | The old wakeup fd is returned (or -1 if file descriptor wakeup was not |
| 341 | enabled). If *fd* is -1, file descriptor wakeup is disabled. |
| 342 | If not -1, *fd* must be non-blocking. It is up to the library to remove |
| 343 | any bytes from *fd* before calling poll or select again. |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 344 | |
| 345 | When threads are enabled, this function can only be called from the main thread; |
| 346 | attempting to call it from other threads will cause a :exc:`ValueError` |
| 347 | exception to be raised. |
| 348 | |
Nathaniel J. Smith | 902ab80 | 2017-12-17 20:10:18 -0800 | [diff] [blame] | 349 | There are two common ways to use this function. In both approaches, |
| 350 | you use the fd to wake up when a signal arrives, but then they |
| 351 | differ in how they determine *which* signal or signals have |
| 352 | arrived. |
| 353 | |
| 354 | In the first approach, we read the data out of the fd's buffer, and |
| 355 | the byte values give you the signal numbers. This is simple, but in |
| 356 | rare cases it can run into a problem: generally the fd will have a |
| 357 | limited amount of buffer space, and if too many signals arrive too |
| 358 | quickly, then the buffer may become full, and some signals may be |
| 359 | lost. If you use this approach, then you should set |
| 360 | ``warn_on_full_buffer=True``, which will at least cause a warning |
| 361 | to be printed to stderr when signals are lost. |
| 362 | |
| 363 | In the second approach, we use the wakeup fd *only* for wakeups, |
| 364 | and ignore the actual byte values. In this case, all we care about |
| 365 | is whether the fd's buffer is empty or non-empty; a full buffer |
| 366 | doesn't indicate a problem at all. If you use this approach, then |
| 367 | you should set ``warn_on_full_buffer=False``, so that your users |
| 368 | are not confused by spurious warning messages. |
| 369 | |
Victor Stinner | 1151710 | 2014-07-29 23:31:34 +0200 | [diff] [blame] | 370 | .. versionchanged:: 3.5 |
| 371 | On Windows, the function now also supports socket handles. |
| 372 | |
Nathaniel J. Smith | 902ab80 | 2017-12-17 20:10:18 -0800 | [diff] [blame] | 373 | .. versionchanged:: 3.7 |
| 374 | Added ``warn_on_full_buffer`` parameter. |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 375 | |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 376 | .. function:: siginterrupt(signalnum, flag) |
| 377 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 378 | Change system call restart behaviour: if *flag* is :const:`False`, system |
| 379 | calls will be restarted when interrupted by signal *signalnum*, otherwise |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 380 | system calls will be interrupted. Returns nothing. |
| 381 | |
| 382 | .. availability:: Unix (see the man page :manpage:`siginterrupt(3)` |
| 383 | for further information). |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 384 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 385 | Note that installing a signal handler with :func:`signal` will reset the |
| 386 | restart behaviour to interruptible by implicitly calling |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 387 | :c:func:`siginterrupt` with a true *flag* value for the given signal. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 388 | |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 389 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 390 | .. function:: signal(signalnum, handler) |
| 391 | |
| 392 | Set the handler for signal *signalnum* to the function *handler*. *handler* can |
| 393 | be a callable Python object taking two arguments (see below), or one of the |
| 394 | special values :const:`signal.SIG_IGN` or :const:`signal.SIG_DFL`. The previous |
| 395 | signal handler will be returned (see the description of :func:`getsignal` |
| 396 | above). (See the Unix man page :manpage:`signal(2)`.) |
| 397 | |
| 398 | When threads are enabled, this function can only be called from the main thread; |
| 399 | attempting to call it from other threads will cause a :exc:`ValueError` |
| 400 | exception to be raised. |
| 401 | |
| 402 | 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] | 403 | stack frame (``None`` or a frame object; for a description of frame objects, |
| 404 | see the :ref:`description in the type hierarchy <frame-objects>` or see the |
| 405 | attribute descriptions in the :mod:`inspect` module). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 406 | |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 407 | On Windows, :func:`signal` can only be called with :const:`SIGABRT`, |
Berker Peksag | 219a012 | 2016-11-25 19:46:57 +0300 | [diff] [blame] | 408 | :const:`SIGFPE`, :const:`SIGILL`, :const:`SIGINT`, :const:`SIGSEGV`, |
| 409 | :const:`SIGTERM`, or :const:`SIGBREAK`. |
| 410 | A :exc:`ValueError` will be raised in any other case. |
Berker Peksag | 77e543c | 2016-04-24 02:59:16 +0300 | [diff] [blame] | 411 | Note that not all systems define the same set of signal names; an |
| 412 | :exc:`AttributeError` will be raised if a signal name is not defined as |
| 413 | ``SIG*`` module level constant. |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 414 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 415 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 416 | .. function:: sigpending() |
| 417 | |
| 418 | Examine the set of signals that are pending for delivery to the calling |
| 419 | thread (i.e., the signals which have been raised while blocked). Return the |
| 420 | set of the pending signals. |
| 421 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 422 | .. availability:: Unix (see the man page :manpage:`sigpending(2)` for further |
| 423 | information). |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 424 | |
| 425 | See also :func:`pause`, :func:`pthread_sigmask` and :func:`sigwait`. |
| 426 | |
| 427 | .. versionadded:: 3.3 |
| 428 | |
| 429 | |
| 430 | .. function:: sigwait(sigset) |
| 431 | |
| 432 | Suspend execution of the calling thread until the delivery of one of the |
| 433 | signals specified in the signal set *sigset*. The function accepts the signal |
| 434 | (removes it from the pending list of signals), and returns the signal number. |
| 435 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 436 | .. availability:: Unix (see the man page :manpage:`sigwait(3)` for further |
| 437 | information). |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 438 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 439 | See also :func:`pause`, :func:`pthread_sigmask`, :func:`sigpending`, |
| 440 | :func:`sigwaitinfo` and :func:`sigtimedwait`. |
| 441 | |
| 442 | .. versionadded:: 3.3 |
| 443 | |
| 444 | |
| 445 | .. function:: sigwaitinfo(sigset) |
| 446 | |
| 447 | Suspend execution of the calling thread until the delivery of one of the |
| 448 | signals specified in the signal set *sigset*. The function accepts the |
| 449 | signal and removes it from the pending list of signals. If one of the |
| 450 | signals in *sigset* is already pending for the calling thread, the function |
| 451 | will return immediately with information about that signal. The signal |
| 452 | handler is not called for the delivered signal. The function raises an |
Antoine Pitrou | 767c0a8 | 2011-10-23 23:52:23 +0200 | [diff] [blame] | 453 | :exc:`InterruptedError` if it is interrupted by a signal that is not in |
| 454 | *sigset*. |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 455 | |
| 456 | The return value is an object representing the data contained in the |
| 457 | :c:type:`siginfo_t` structure, namely: :attr:`si_signo`, :attr:`si_code`, |
| 458 | :attr:`si_errno`, :attr:`si_pid`, :attr:`si_uid`, :attr:`si_status`, |
| 459 | :attr:`si_band`. |
| 460 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 461 | .. availability:: Unix (see the man page :manpage:`sigwaitinfo(2)` for further |
| 462 | information). |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 463 | |
| 464 | See also :func:`pause`, :func:`sigwait` and :func:`sigtimedwait`. |
| 465 | |
| 466 | .. versionadded:: 3.3 |
| 467 | |
Victor Stinner | a453cd8 | 2015-03-20 12:54:28 +0100 | [diff] [blame] | 468 | .. versionchanged:: 3.5 |
| 469 | The function is now retried if interrupted by a signal not in *sigset* |
| 470 | and the signal handler does not raise an exception (see :pep:`475` for |
| 471 | the rationale). |
| 472 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 473 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 474 | .. function:: sigtimedwait(sigset, timeout) |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 475 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 476 | Like :func:`sigwaitinfo`, but takes an additional *timeout* argument |
| 477 | specifying a timeout. If *timeout* is specified as :const:`0`, a poll is |
| 478 | performed. Returns :const:`None` if a timeout occurs. |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 479 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 480 | .. availability:: Unix (see the man page :manpage:`sigtimedwait(2)` for further |
| 481 | information). |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 482 | |
| 483 | See also :func:`pause`, :func:`sigwait` and :func:`sigwaitinfo`. |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 484 | |
| 485 | .. versionadded:: 3.3 |
| 486 | |
Victor Stinner | a453cd8 | 2015-03-20 12:54:28 +0100 | [diff] [blame] | 487 | .. versionchanged:: 3.5 |
Victor Stinner | eb011cb | 2015-03-31 12:19:15 +0200 | [diff] [blame] | 488 | The function is now retried with the recomputed *timeout* if interrupted |
| 489 | by a signal not in *sigset* and the signal handler does not raise an |
Victor Stinner | a453cd8 | 2015-03-20 12:54:28 +0100 | [diff] [blame] | 490 | exception (see :pep:`475` for the rationale). |
| 491 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 492 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 493 | .. _signal-example: |
| 494 | |
| 495 | Example |
| 496 | ------- |
| 497 | |
| 498 | Here is a minimal example program. It uses the :func:`alarm` function to limit |
| 499 | the time spent waiting to open a file; this is useful if the file is for a |
| 500 | serial device that may not be turned on, which would normally cause the |
| 501 | :func:`os.open` to hang indefinitely. The solution is to set a 5-second alarm |
| 502 | before opening the file; if the operation takes too long, the alarm signal will |
| 503 | be sent, and the handler raises an exception. :: |
| 504 | |
| 505 | import signal, os |
| 506 | |
| 507 | def handler(signum, frame): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 508 | print('Signal handler called with signal', signum) |
Antoine Pitrou | 4272d6a | 2011-10-12 19:10:10 +0200 | [diff] [blame] | 509 | raise OSError("Couldn't open device!") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 510 | |
| 511 | # Set the signal handler and a 5-second alarm |
| 512 | signal.signal(signal.SIGALRM, handler) |
| 513 | signal.alarm(5) |
| 514 | |
| 515 | # This open() may hang indefinitely |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 516 | fd = os.open('/dev/ttyS0', os.O_RDWR) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 517 | |
| 518 | signal.alarm(0) # Disable the alarm |
| 519 | |
Alfred Perlstein | a251073 | 2018-08-17 09:48:05 -0400 | [diff] [blame] | 520 | Note on SIGPIPE |
| 521 | --------------- |
| 522 | |
| 523 | Piping output of your program to tools like :manpage:`head(1)` will |
| 524 | cause a :const:`SIGPIPE` signal to be sent to your process when the receiver |
| 525 | of its standard output closes early. This results in an exception |
| 526 | like :code:`BrokenPipeError: [Errno 32] Broken pipe`. To handle this |
| 527 | case, wrap your entry point to catch this exception as follows:: |
| 528 | |
| 529 | import os |
| 530 | import sys |
| 531 | |
| 532 | def main(): |
| 533 | try: |
| 534 | # simulate large output (your code replaces this loop) |
| 535 | for x in range(10000): |
| 536 | print("y") |
| 537 | # flush output here to force SIGPIPE to be triggered |
| 538 | # while inside this try block. |
| 539 | sys.stdout.flush() |
| 540 | except BrokenPipeError: |
| 541 | # Python flushes standard streams on exit; redirect remaining output |
| 542 | # to devnull to avoid another BrokenPipeError at shutdown |
| 543 | devnull = os.open(os.devnull, os.O_WRONLY) |
| 544 | os.dup2(devnull, sys.stdout.fileno()) |
| 545 | sys.exit(1) # Python exits with error code 1 on EPIPE |
| 546 | |
| 547 | if __name__ == '__main__': |
| 548 | main() |
| 549 | |
| 550 | Do not set :const:`SIGPIPE`'s disposition to :const:`SIG_DFL` |
| 551 | in order to avoid :exc:`BrokenPipeError`. Doing that would cause |
| 552 | your program to exit unexpectedly also whenever any socket connection |
| 553 | is interrupted while your program is still writing to it. |