Lines Matching full:signal

1 :mod:`signal` --- Set handlers for asynchronous events
4 .. module:: signal
7 **Source code:** :source:`Lib/signal.py`
11 This module provides mechanisms to use signal handlers in Python.
17 The :func:`signal.signal` function allows defining custom handlers to be
18 executed when a signal is received. A small number of default handlers are
24 A handler for a particular signal, once set, remains installed until it is
33 Execution of Python signal handlers
36 A Python signal handler does not get executed inside the low-level (C) signal
37 handler. Instead, the low-level signal handler sets a flag which tells the
38 :term:`virtual machine` to execute the corresponding Python signal handler
44 will return from the signal handler to the C code, which is likely to raise
45 the same signal again, causing Python to apparently hang. From Python 3.3
52 signal handlers will be called when the calculation finishes.
64 Python signal handlers are always executed in the main Python thread of the main interpreter,
65 even if the signal was received in another thread. This means that signals
69 Besides, only the main thread of the main interpreter is allowed to set a new signal handler.
76 signal (SIG*), handler (:const:`SIG_DFL`, :const:`SIG_IGN`) and sigmask
85 The signal module defines three enums:
111 The variables defined in the :mod:`signal` module are:
116 This is one of two standard signal handling options; it will simply perform
117 the default function for the signal. For example, on most systems the
124 This is another standard signal handler, which will simply ignore the given
125 signal.
130 Abort signal from :manpage:`abort(3)`.
134 Timer signal from :manpage:`alarm(2)`.
192 Kill signal.
202 Default action is to ignore the signal.
212 Stack fault on coprocessor. The Linux kernel does not raise this signal: it
217 On architectures where the signal is available. See
218 the man page :manpage:`signal(7)` for further information.
224 Termination signal.
228 User-defined signal 1.
234 User-defined signal 2.
240 Window resize signal.
246 All the signal numbers are defined symbolically. For example, the hangup signal
247 is defined as :const:`signal.SIGHUP`; the variable names are identical to the
248 names used in C programs, as found in ``<signal.h>``. The Unix man page for
249 ':c:func:`signal`' lists the existing signals (on some systems this is
250 :manpage:`signal(2)`, on others the list is in :manpage:`signal(7)`). Note that
251 not all systems define the same set of signal names; only those names defined by
257 The signal corresponding to the :kbd:`Ctrl+C` keystroke event. This signal can
267 The signal corresponding to the :kbd:`Ctrl+Break` keystroke event. This signal can
277 One more than the number of the highest signal number.
278 Use :func:`valid_signals` to get valid signal numbers.
318 indicating that the signal mask is to be replaced.
323 The :mod:`signal` module defines one exception:
327 Raised to signal an error from the underlying :func:`setitimer` or
337 The :mod:`signal` module defines the following functions:
342 If *time* is non-zero, this function requests that a :const:`SIGALRM` signal be
356 Return the current signal handler for the signal *signalnum*. The returned value
358 :const:`signal.SIG_IGN`, :const:`signal.SIG_DFL` or :const:`None`. Here,
359 :const:`signal.SIG_IGN` means that the signal was previously ignored,
360 :const:`signal.SIG_DFL` means that the default way of handling the signal was
361 previously in use, and ``None`` means that the previous signal handler was not
367 Returns the description of signal *signalnum*, such as "Interrupt"
376 Return the set of valid signal numbers on this platform. This can be
385 Cause the process to sleep until a signal is received; the appropriate handler
390 See the man page :manpage:`signal(2)` for further information.
398 Sends a signal to the calling process. Returns nothing.
405 Send signal *sig* to the process referred to by file descriptor *pidfd*.
418 Send the signal *signalnum* to the thread *thread_id*, another thread in the
421 interpreter, the Python signal handlers will be :ref:`executed by the main
423 signal to a particular Python thread would be to force a running system call
430 If *signalnum* is 0, then no signal is sent, but error checking is still
433 .. audit-event:: signal.pthread_kill thread_id,signalnum signal.pthread_kill
446 Fetch and/or change the signal mask of the calling thread. The signal mask
448 Return the old signal mask as a set of signals.
456 signal which is not blocked.
460 *mask* is a set of signal numbers (e.g. {:const:`signal.SIGINT`,
461 :const:`signal.SIGTERM`}). Use :func:`~signal.valid_signals` for a full
464 For example, ``signal.pthread_sigmask(signal.SIG_BLOCK, [])`` reads the
465 signal mask of the calling thread.
481 Sets given interval timer (one of :const:`signal.ITIMER_REAL`,
482 :const:`signal.ITIMER_VIRTUAL` or :const:`signal.ITIMER_PROF`) specified
488 When an interval timer fires, a signal is sent to the process.
489 The signal sent is dependent on the timer being used;
490 :const:`signal.ITIMER_REAL` will deliver :const:`SIGALRM`,
491 :const:`signal.ITIMER_VIRTUAL` sends :const:`SIGVTALRM`,
492 and :const:`signal.ITIMER_PROF` will deliver :const:`SIGPROF`.
511 Set the wakeup file descriptor to *fd*. When a signal is received, the
512 signal number is written as a single byte into the fd. This can be used by
513 a library to wakeup a poll or select call, allowing the signal to be fully
527 you use the fd to wake up when a signal arrives, but then they
528 differ in how they determine *which* signal or signals have
532 the byte values give you the signal numbers. This is simple, but in
556 calls will be restarted when interrupted by signal *signalnum*, otherwise
563 Note that installing a signal handler with :func:`signal` will reset the
565 :c:func:`siginterrupt` with a true *flag* value for the given signal.
568 .. function:: signal(signalnum, handler)
570 Set the handler for signal *signalnum* to the function *handler*. *handler* can
572 special values :const:`signal.SIG_IGN` or :const:`signal.SIG_DFL`. The previous
573 signal handler will be returned (see the description of :func:`getsignal`
574 above). (See the Unix man page :manpage:`signal(2)` for further information.)
581 The *handler* is called with two arguments: the signal number and the current
586 On Windows, :func:`signal` can only be called with :const:`SIGABRT`,
590 Note that not all systems define the same set of signal names; an
591 :exc:`AttributeError` will be raised if a signal name is not defined as
613 signals specified in the signal set *sigset*. The function accepts the signal
614 (removes it from the pending list of signals), and returns the signal number.
629 signals specified in the signal set *sigset*. The function accepts the
630 signal and removes it from the pending list of signals. If one of the
632 will return immediately with information about that signal. The signal
633 handler is not called for the delivered signal. The function raises an
634 :exc:`InterruptedError` if it is interrupted by a signal that is not in
651 The function is now retried if interrupted by a signal not in *sigset*
652 and the signal handler does not raise an exception (see :pep:`475` for
672 by a signal not in *sigset* and the signal handler does not raise an
685 before opening the file; if the operation takes too long, the alarm signal will
688 import signal, os
691 signame = signal.Signals(signum).name
692 print(f'Signal handler called with signal {signame} ({signum})')
695 # Set the signal handler and a 5-second alarm
696 signal.signal(signal.SIGALRM, handler)
697 signal.alarm(5)
702 signal.alarm(0) # Disable the alarm
708 cause a :const:`SIGPIPE` signal to be sent to your process when the receiver
742 Note on Signal Handlers and Exceptions
745 If a signal handler raises an exception, the exception will be propagated to
750 a signal handler) may on rare occasions put the program in an unexpected state.
771 complex or require high reliability should avoid raising exceptions from signal
777 import signal
785 print('Signal handler called with signal', signum)
787 signal.signal(signal.SIGINT, handler)