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