Home | History | Annotate | Download | only in library

Lines Matching full:signal

1 :mod:`signal` --- Set handlers for asynchronous events
4 .. module:: signal
9 This module provides mechanisms to use signal handlers in Python.
15 The :func:`signal.signal` function allows defining custom handlers to be
16 executed when a signal is received. A small number of default handlers are
21 A handler for a particular signal, once set, remains installed until it is
27 Execution of Python signal handlers
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
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
46 signal handlers will be called when the calculation finishes.
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
60 Besides, only the main thread is allowed to set a new signal handler.
67 signal (SIG*), handler (:const:`SIG_DFL`, :const:`SIG_IGN`) and sigmask
76 The variables defined in the :mod:`signal` module are:
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
89 This is another standard signal handler, which will simply ignore the given
90 signal.
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
98 ':c:func:`signal`' lists the existing signals (on some systems this is
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
106 The signal corresponding to the :kbd:`Ctrl+C` keystroke event. This signal can
116 The signal corresponding to the :kbd:`Ctrl+Break` keystroke event. This signal can
126 One more than the number of the highest signal number.
166 indicating that the signal mask is to be replaced.
171 The :mod:`signal` module defines one exception:
175 Raised to signal an error from the underlying :func:`setitimer` or
185 The :mod:`signal` module defines the following functions:
190 If *time* is non-zero, this function requests that a :const:`SIGALRM` signal be
201 Return the current signal handler for the signal *signalnum*. The returned value
203 :const:`signal.SIG_IGN`, :const:`signal.SIG_DFL` or :const:`None`. Here,
204 :const:`signal.SIG_IGN` means that the signal was previously ignored,
205 :const:`signal.SIG_DFL` means that the default way of handling the signal was
206 previously in use, and ``None`` means that the previous signal handler was not
212 Cause the process to sleep until a signal is received; the appropriate handler
214 :manpage:`signal(2)`.)
222 Send the signal *signalnum* to the thread *thread_id*, another thread in the
225 interpreter, the Python signal handlers will be :ref:`executed by the main
227 signal to a particular Python thread would be to force a running system call
234 If *signalnum* is 0, then no signal is sent, but error checking is still
247 Fetch and/or change the signal mask of the calling thread. The signal mask
249 Return the old signal mask as a set of signals.
257 signal which is not blocked.
261 *mask* is a set of signal numbers (e.g. {:const:`signal.SIGINT`,
262 :const:`signal.SIGTERM`}). Use ``range(1, signal.NSIG)`` for a full mask
265 For example, ``signal.pthread_sigmask(signal.SIG_BLOCK, [])`` reads the
266 signal mask of the calling thread.
278 Sets given interval timer (one of :const:`signal.ITIMER_REAL`,
279 :const:`signal.ITIMER_VIRTUAL` or :const:`signal.ITIMER_PROF`) specified
284 When an interval timer fires, a signal is sent to the process.
285 The signal sent is dependent on the timer being used;
286 :const:`signal.ITIMER_REAL` will deliver :const:`SIGALRM`,
287 :const:`signal.ITIMER_VIRTUAL` sends :const:`SIGVTALRM`,
288 and :const:`signal.ITIMER_PROF` will deliver :const:`SIGPROF`.
304 Set the wakeup file descriptor to *fd*. When a signal is received, the
305 signal number is written as a single byte into the fd. This can be used by
306 a library to wakeup a poll or select call, allowing the signal to be fully
313 signal numbers list.
326 calls will be restarted when interrupted by signal *signalnum*, otherwise
330 Note that installing a signal handler with :func:`signal` will reset the
332 :c:func:`siginterrupt` with a true *flag* value for the given signal.
335 .. function:: signal(signalnum, handler)
337 Set the handler for signal *signalnum* to the function *handler*. *handler* can
339 special values :const:`signal.SIG_IGN` or :const:`signal.SIG_DFL`. The previous
340 signal handler will be returned (see the description of :func:`getsignal`
341 above). (See the Unix man page :manpage:`signal(2)`.)
347 The *handler* is called with two arguments: the signal number and the current
352 On Windows, :func:`signal` can only be called with :const:`SIGABRT`,
356 Note that not all systems define the same set of signal names; an
357 :exc:`AttributeError` will be raised if a signal name is not defined as
378 signals specified in the signal set *sigset*. The function accepts the signal
379 (removes it from the pending list of signals), and returns the signal number.
393 signals specified in the signal set *sigset*. The function accepts the
394 signal and removes it from the pending list of signals. If one of the
396 will return immediately with information about that signal. The signal
397 handler is not called for the delivered signal. The function raises an
398 :exc:`InterruptedError` if it is interrupted by a signal that is not in
414 The function is now retried if interrupted by a signal not in *sigset*
415 and the signal handler does not raise an exception (see :pep:`475` for
434 by a signal not in *sigset* and the signal handler does not raise an
447 before opening the file; if the operation takes too long, the alarm signal will
450 import signal, os
453 print('Signal handler called with signal', signum)
456 # Set the signal handler and a 5-second alarm
457 signal.signal(signal.SIGALRM, handler)
458 signal.alarm(5)
463 signal.alarm(0) # Disable the alarm