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 | |
| 8 | This module provides mechanisms to use signal handlers in Python. Some general |
| 9 | rules for working with signals and their handlers: |
| 10 | |
| 11 | * A handler for a particular signal, once set, remains installed until it is |
| 12 | explicitly reset (Python emulates the BSD style interface regardless of the |
| 13 | underlying implementation), with the exception of the handler for |
| 14 | :const:`SIGCHLD`, which follows the underlying implementation. |
| 15 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | * Although Python signal handlers are called asynchronously as far as the Python |
| 17 | user is concerned, they can only occur between the "atomic" instructions of the |
| 18 | Python interpreter. This means that signals arriving during long calculations |
| 19 | implemented purely in C (such as regular expression matches on large bodies of |
| 20 | text) may be delayed for an arbitrary amount of time. |
| 21 | |
| 22 | * When a signal arrives during an I/O operation, it is possible that the I/O |
| 23 | operation raises an exception after the signal handler returns. This is |
| 24 | dependent on the underlying Unix system's semantics regarding interrupted system |
| 25 | calls. |
| 26 | |
| 27 | * Because the C signal handler always returns, it makes little sense to catch |
| 28 | synchronous errors like :const:`SIGFPE` or :const:`SIGSEGV`. |
| 29 | |
| 30 | * Python installs a small number of signal handlers by default: :const:`SIGPIPE` |
| 31 | is ignored (so write errors on pipes and sockets can be reported as ordinary |
| 32 | Python exceptions) and :const:`SIGINT` is translated into a |
| 33 | :exc:`KeyboardInterrupt` exception. All of these can be overridden. |
| 34 | |
| 35 | * Some care must be taken if both signals and threads are used in the same |
| 36 | program. The fundamental thing to remember in using signals and threads |
| 37 | simultaneously is: always perform :func:`signal` operations in the main thread |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 38 | of execution. Any thread can perform an :func:`alarm`, :func:`getsignal`, |
| 39 | :func:`pause`, :func:`setitimer` or :func:`getitimer`; only the main thread |
| 40 | can set a new signal handler, and the main thread will be the only one to |
| 41 | receive signals (this is enforced by the Python :mod:`signal` module, even |
| 42 | if the underlying thread implementation supports sending signals to |
| 43 | individual threads). This means that signals can't be used as a means of |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 44 | inter-thread communication. Use locks instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
| 46 | The variables defined in the :mod:`signal` module are: |
| 47 | |
| 48 | |
| 49 | .. data:: SIG_DFL |
| 50 | |
Benjamin Peterson | 6ebe78f | 2008-12-21 00:06:59 +0000 | [diff] [blame] | 51 | This is one of two standard signal handling options; it will simply perform |
| 52 | the default function for the signal. For example, on most systems the |
| 53 | default action for :const:`SIGQUIT` is to dump core and exit, while the |
| 54 | default action for :const:`SIGCHLD` is to simply ignore it. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | .. data:: SIG_IGN |
| 58 | |
| 59 | This is another standard signal handler, which will simply ignore the given |
| 60 | signal. |
| 61 | |
| 62 | |
| 63 | .. data:: SIG* |
| 64 | |
| 65 | All the signal numbers are defined symbolically. For example, the hangup signal |
| 66 | is defined as :const:`signal.SIGHUP`; the variable names are identical to the |
| 67 | 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] | 68 | ':c:func:`signal`' lists the existing signals (on some systems this is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | :manpage:`signal(2)`, on others the list is in :manpage:`signal(7)`). Note that |
| 70 | not all systems define the same set of signal names; only those names defined by |
| 71 | the system are defined by this module. |
| 72 | |
| 73 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 74 | .. data:: CTRL_C_EVENT |
| 75 | |
Brian Curtin | f045d77 | 2010-08-05 18:56:00 +0000 | [diff] [blame] | 76 | The signal corresponding to the CTRL+C keystroke event. This signal can |
| 77 | only be used with :func:`os.kill`. |
| 78 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 79 | Availability: Windows. |
| 80 | |
Brian Curtin | 904bd39 | 2010-04-20 15:28:06 +0000 | [diff] [blame] | 81 | .. versionadded:: 3.2 |
| 82 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 83 | |
| 84 | .. data:: CTRL_BREAK_EVENT |
| 85 | |
Brian Curtin | f045d77 | 2010-08-05 18:56:00 +0000 | [diff] [blame] | 86 | The signal corresponding to the CTRL+BREAK keystroke event. This signal can |
| 87 | only be used with :func:`os.kill`. |
| 88 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 89 | Availability: Windows. |
| 90 | |
Brian Curtin | 904bd39 | 2010-04-20 15:28:06 +0000 | [diff] [blame] | 91 | .. versionadded:: 3.2 |
| 92 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 93 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | .. data:: NSIG |
| 95 | |
| 96 | One more than the number of the highest signal number. |
| 97 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 98 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 99 | .. data:: ITIMER_REAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 100 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 101 | Decrements interval timer in real time, and delivers :const:`SIGALRM` upon |
| 102 | expiration. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 103 | |
| 104 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 105 | .. data:: ITIMER_VIRTUAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 106 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 107 | 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] | 108 | SIGVTALRM upon expiration. |
| 109 | |
| 110 | |
| 111 | .. data:: ITIMER_PROF |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 112 | |
| 113 | Decrements interval timer both when the process executes and when the |
| 114 | system is executing on behalf of the process. Coupled with ITIMER_VIRTUAL, |
| 115 | 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] | 116 | in user and kernel space. SIGPROF is delivered upon expiration. |
| 117 | |
| 118 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 119 | .. data:: SIG_BLOCK |
| 120 | |
| 121 | A possible value for the *how* parameter to :func:`pthread_sigmask` |
| 122 | indicating that signals are to be blocked. |
| 123 | |
| 124 | .. versionadded:: 3.3 |
| 125 | |
| 126 | .. data:: SIG_UNBLOCK |
| 127 | |
| 128 | A possible value for the *how* parameter to :func:`pthread_sigmask` |
| 129 | indicating that signals are to be unblocked. |
| 130 | |
| 131 | .. versionadded:: 3.3 |
| 132 | |
| 133 | .. data:: SIG_SETMASK |
| 134 | |
| 135 | A possible value for the *how* parameter to :func:`pthread_sigmask` |
| 136 | indicating that the signal mask is to be replaced. |
| 137 | |
| 138 | .. versionadded:: 3.3 |
| 139 | |
| 140 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 141 | The :mod:`signal` module defines one exception: |
| 142 | |
| 143 | .. exception:: ItimerError |
| 144 | |
| 145 | Raised to signal an error from the underlying :func:`setitimer` or |
| 146 | :func:`getitimer` implementation. Expect this error if an invalid |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 147 | interval timer or a negative time is passed to :func:`setitimer`. |
Antoine Pitrou | 4272d6a | 2011-10-12 19:10:10 +0200 | [diff] [blame] | 148 | This error is a subtype of :exc:`OSError`. |
| 149 | |
| 150 | .. versionadded:: 3.3 |
| 151 | This error used to be a subtype of :exc:`IOError`, which is now an |
| 152 | alias of :exc:`OSError`. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 153 | |
| 154 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | The :mod:`signal` module defines the following functions: |
| 156 | |
| 157 | |
| 158 | .. function:: alarm(time) |
| 159 | |
| 160 | If *time* is non-zero, this function requests that a :const:`SIGALRM` signal be |
| 161 | sent to the process in *time* seconds. Any previously scheduled alarm is |
| 162 | canceled (only one alarm can be scheduled at any time). The returned value is |
| 163 | then the number of seconds before any previously set alarm was to have been |
| 164 | delivered. If *time* is zero, no alarm is scheduled, and any scheduled alarm is |
| 165 | canceled. If the return value is zero, no alarm is currently scheduled. (See |
| 166 | the Unix man page :manpage:`alarm(2)`.) Availability: Unix. |
| 167 | |
| 168 | |
| 169 | .. function:: getsignal(signalnum) |
| 170 | |
| 171 | Return the current signal handler for the signal *signalnum*. The returned value |
| 172 | may be a callable Python object, or one of the special values |
| 173 | :const:`signal.SIG_IGN`, :const:`signal.SIG_DFL` or :const:`None`. Here, |
| 174 | :const:`signal.SIG_IGN` means that the signal was previously ignored, |
| 175 | :const:`signal.SIG_DFL` means that the default way of handling the signal was |
| 176 | previously in use, and ``None`` means that the previous signal handler was not |
| 177 | installed from Python. |
| 178 | |
| 179 | |
| 180 | .. function:: pause() |
| 181 | |
| 182 | Cause the process to sleep until a signal is received; the appropriate handler |
| 183 | will then be called. Returns nothing. Not on Windows. (See the Unix man page |
| 184 | :manpage:`signal(2)`.) |
| 185 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 186 | See also :func:`sigwait`, :func:`sigwaitinfo`, :func:`sigtimedwait` and |
| 187 | :func:`sigpending`. |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 188 | |
| 189 | |
| 190 | .. function:: pthread_kill(thread_id, signum) |
| 191 | |
| 192 | Send the signal *signum* to the thread *thread_id*, another thread in the same |
| 193 | process as the caller. The signal is asynchronously directed to thread. |
| 194 | |
Victor Stinner | 2a12974 | 2011-05-30 23:02:52 +0200 | [diff] [blame] | 195 | Use :func:`threading.get_ident()` or the :attr:`~threading.Thread.ident` |
| 196 | attribute of :attr:`threading.Thread` to get a 'thread identifier' for |
| 197 | *thread_id*. |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 198 | |
| 199 | If *signum* is 0, then no signal is sent, but error checking is still |
| 200 | performed; this can be used to check if a thread is still running. |
| 201 | |
| 202 | Availability: Unix (see the man page :manpage:`pthread_kill(3)` for further |
| 203 | information). |
| 204 | |
| 205 | See also :func:`os.kill`. |
| 206 | |
| 207 | .. versionadded:: 3.3 |
| 208 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 210 | .. function:: pthread_sigmask(how, mask) |
| 211 | |
| 212 | Fetch and/or change the signal mask of the calling thread. The signal mask |
| 213 | 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] | 214 | Return the old signal mask as a set of signals. |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 215 | |
| 216 | The behavior of the call is dependent on the value of *how*, as follows. |
| 217 | |
| 218 | * :data:`SIG_BLOCK`: The set of blocked signals is the union of the current |
| 219 | set and the *mask* argument. |
| 220 | * :data:`SIG_UNBLOCK`: The signals in *mask* are removed from the current |
| 221 | set of blocked signals. It is permissible to attempt to unblock a |
| 222 | signal which is not blocked. |
| 223 | * :data:`SIG_SETMASK`: The set of blocked signals is set to the *mask* |
| 224 | argument. |
| 225 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 226 | *mask* is a set of signal numbers (e.g. {:const:`signal.SIGINT`, |
| 227 | :const:`signal.SIGTERM`}). Use ``range(1, signal.NSIG)`` for a full mask |
| 228 | including all signals. |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 229 | |
| 230 | For example, ``signal.pthread_sigmask(signal.SIG_BLOCK, [])`` reads the |
| 231 | signal mask of the calling thread. |
| 232 | |
| 233 | Availability: Unix. See the man page :manpage:`sigprocmask(3)` and |
| 234 | :manpage:`pthread_sigmask(3)` for further information. |
| 235 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 236 | See also :func:`pause`, :func:`sigpending` and :func:`sigwait`. |
| 237 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 238 | .. versionadded:: 3.3 |
| 239 | |
| 240 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 241 | .. function:: setitimer(which, seconds[, interval]) |
| 242 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 243 | Sets given interval timer (one of :const:`signal.ITIMER_REAL`, |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 244 | :const:`signal.ITIMER_VIRTUAL` or :const:`signal.ITIMER_PROF`) specified |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 245 | 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] | 246 | :func:`alarm`) and after that every *interval* seconds. The interval |
| 247 | timer specified by *which* can be cleared by setting seconds to zero. |
| 248 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 249 | When an interval timer fires, a signal is sent to the process. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 250 | The signal sent is dependent on the timer being used; |
| 251 | :const:`signal.ITIMER_REAL` will deliver :const:`SIGALRM`, |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 252 | :const:`signal.ITIMER_VIRTUAL` sends :const:`SIGVTALRM`, |
| 253 | and :const:`signal.ITIMER_PROF` will deliver :const:`SIGPROF`. |
| 254 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 255 | The old values are returned as a tuple: (delay, interval). |
| 256 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 257 | Attempting to pass an invalid interval timer will cause an |
| 258 | :exc:`ItimerError`. Availability: Unix. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 259 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 260 | |
| 261 | .. function:: getitimer(which) |
| 262 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 263 | Returns current value of a given interval timer specified by *which*. |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 264 | Availability: Unix. |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 265 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 266 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 267 | .. function:: set_wakeup_fd(fd) |
| 268 | |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 269 | Set the wakeup file descriptor to *fd*. When a signal is received, the |
| 270 | signal number is written as a single byte into the fd. This can be used by |
| 271 | a library to wakeup a poll or select call, allowing the signal to be fully |
| 272 | processed. |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 273 | |
| 274 | The old wakeup fd is returned. *fd* must be non-blocking. It is up to the |
| 275 | library to remove any bytes before calling poll or select again. |
| 276 | |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 277 | Use for example ``struct.unpack('%uB' % len(data), data)`` to decode the |
| 278 | signal numbers list. |
| 279 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 280 | When threads are enabled, this function can only be called from the main thread; |
| 281 | attempting to call it from other threads will cause a :exc:`ValueError` |
| 282 | exception to be raised. |
| 283 | |
| 284 | |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 285 | .. function:: siginterrupt(signalnum, flag) |
| 286 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 287 | Change system call restart behaviour: if *flag* is :const:`False`, system |
| 288 | calls will be restarted when interrupted by signal *signalnum*, otherwise |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 289 | system calls will be interrupted. Returns nothing. Availability: Unix (see |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 290 | the man page :manpage:`siginterrupt(3)` for further information). |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 291 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 292 | Note that installing a signal handler with :func:`signal` will reset the |
| 293 | restart behaviour to interruptible by implicitly calling |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 294 | :c:func:`siginterrupt` with a true *flag* value for the given signal. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 295 | |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 296 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 297 | .. function:: signal(signalnum, handler) |
| 298 | |
| 299 | Set the handler for signal *signalnum* to the function *handler*. *handler* can |
| 300 | be a callable Python object taking two arguments (see below), or one of the |
| 301 | special values :const:`signal.SIG_IGN` or :const:`signal.SIG_DFL`. The previous |
| 302 | signal handler will be returned (see the description of :func:`getsignal` |
| 303 | above). (See the Unix man page :manpage:`signal(2)`.) |
| 304 | |
| 305 | When threads are enabled, this function can only be called from the main thread; |
| 306 | attempting to call it from other threads will cause a :exc:`ValueError` |
| 307 | exception to be raised. |
| 308 | |
| 309 | 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] | 310 | stack frame (``None`` or a frame object; for a description of frame objects, |
| 311 | see the :ref:`description in the type hierarchy <frame-objects>` or see the |
| 312 | attribute descriptions in the :mod:`inspect` module). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 | |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 314 | On Windows, :func:`signal` can only be called with :const:`SIGABRT`, |
| 315 | :const:`SIGFPE`, :const:`SIGILL`, :const:`SIGINT`, :const:`SIGSEGV`, or |
| 316 | :const:`SIGTERM`. A :exc:`ValueError` will be raised in any other case. |
| 317 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 318 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 319 | .. function:: sigpending() |
| 320 | |
| 321 | Examine the set of signals that are pending for delivery to the calling |
| 322 | thread (i.e., the signals which have been raised while blocked). Return the |
| 323 | set of the pending signals. |
| 324 | |
| 325 | Availability: Unix (see the man page :manpage:`sigpending(2)` for further |
| 326 | information). |
| 327 | |
| 328 | See also :func:`pause`, :func:`pthread_sigmask` and :func:`sigwait`. |
| 329 | |
| 330 | .. versionadded:: 3.3 |
| 331 | |
| 332 | |
| 333 | .. function:: sigwait(sigset) |
| 334 | |
| 335 | Suspend execution of the calling thread until the delivery of one of the |
| 336 | signals specified in the signal set *sigset*. The function accepts the signal |
| 337 | (removes it from the pending list of signals), and returns the signal number. |
| 338 | |
| 339 | Availability: Unix (see the man page :manpage:`sigwait(3)` for further |
| 340 | information). |
| 341 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 342 | See also :func:`pause`, :func:`pthread_sigmask`, :func:`sigpending`, |
| 343 | :func:`sigwaitinfo` and :func:`sigtimedwait`. |
| 344 | |
| 345 | .. versionadded:: 3.3 |
| 346 | |
| 347 | |
| 348 | .. function:: sigwaitinfo(sigset) |
| 349 | |
| 350 | Suspend execution of the calling thread until the delivery of one of the |
| 351 | signals specified in the signal set *sigset*. The function accepts the |
| 352 | signal and removes it from the pending list of signals. If one of the |
| 353 | signals in *sigset* is already pending for the calling thread, the function |
| 354 | will return immediately with information about that signal. The signal |
| 355 | handler is not called for the delivered signal. The function raises an |
Antoine Pitrou | 767c0a8 | 2011-10-23 23:52:23 +0200 | [diff] [blame] | 356 | :exc:`InterruptedError` if it is interrupted by a signal that is not in |
| 357 | *sigset*. |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 358 | |
| 359 | The return value is an object representing the data contained in the |
| 360 | :c:type:`siginfo_t` structure, namely: :attr:`si_signo`, :attr:`si_code`, |
| 361 | :attr:`si_errno`, :attr:`si_pid`, :attr:`si_uid`, :attr:`si_status`, |
| 362 | :attr:`si_band`. |
| 363 | |
| 364 | Availability: Unix (see the man page :manpage:`sigwaitinfo(2)` for further |
| 365 | information). |
| 366 | |
| 367 | See also :func:`pause`, :func:`sigwait` and :func:`sigtimedwait`. |
| 368 | |
| 369 | .. versionadded:: 3.3 |
| 370 | |
| 371 | |
| 372 | .. function:: sigtimedwait(sigset, (timeout_sec, timeout_nsec)) |
| 373 | |
| 374 | Like :func:`sigtimedwait`, but takes a tuple of ``(seconds, nanoseconds)`` |
| 375 | as an additional argument specifying a timeout. If both *timeout_sec* and |
| 376 | *timeout_nsec* are specified as :const:`0`, a poll is performed. Returns |
| 377 | :const:`None` if a timeout occurs. |
| 378 | |
| 379 | Availability: Unix (see the man page :manpage:`sigtimedwait(2)` for further |
| 380 | information). |
| 381 | |
| 382 | See also :func:`pause`, :func:`sigwait` and :func:`sigwaitinfo`. |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 383 | |
| 384 | .. versionadded:: 3.3 |
| 385 | |
| 386 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | .. _signal-example: |
| 388 | |
| 389 | Example |
| 390 | ------- |
| 391 | |
| 392 | Here is a minimal example program. It uses the :func:`alarm` function to limit |
| 393 | the time spent waiting to open a file; this is useful if the file is for a |
| 394 | serial device that may not be turned on, which would normally cause the |
| 395 | :func:`os.open` to hang indefinitely. The solution is to set a 5-second alarm |
| 396 | before opening the file; if the operation takes too long, the alarm signal will |
| 397 | be sent, and the handler raises an exception. :: |
| 398 | |
| 399 | import signal, os |
| 400 | |
| 401 | def handler(signum, frame): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 402 | print('Signal handler called with signal', signum) |
Antoine Pitrou | 4272d6a | 2011-10-12 19:10:10 +0200 | [diff] [blame] | 403 | raise OSError("Couldn't open device!") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 404 | |
| 405 | # Set the signal handler and a 5-second alarm |
| 406 | signal.signal(signal.SIGALRM, handler) |
| 407 | signal.alarm(5) |
| 408 | |
| 409 | # This open() may hang indefinitely |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 410 | fd = os.open('/dev/ttyS0', os.O_RDWR) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
| 412 | signal.alarm(0) # Disable the alarm |
| 413 | |