_signal - This module provides mechanisms to use signal handlers in Python.
| 🎯 Use Case | ⚙️ Command | 📝 Description |
|---|---|---|
| Set a signal handler | signal(signalnum, handler) | Set the action for a given signal |
| Get current handler | getsignal(signalnum) | Return the current action for the given signal |
| Alarm after delay | alarm(seconds) | Cause SIGALRM after specified time (Unix only) |
| Wait for signal | pause() | Wait until a signal arrives (Unix only) |
| Set interval timer | setitimer(which, seconds, interval) | Set an itimer with initial and interval time |
| Send signal to thread | pthread_kill(thread_id, signalnum) | Send a signal to a thread |
| Block/unblock signals | pthread_sigmask(how, mask) | Fetch/change signal mask of calling thread |
| Wakeup fd on signal | set_wakeup_fd(fd) | Set fd to be written when signal arrives |
Functions:
alarm() -- cause SIGALRM after a specified time [Unix only]setitimer() -- cause a signal (described below) after a specified float time and the timer may restart then [Unix only]getitimer() -- get current value of timer [Unix only]signal() -- set the action for a given signalgetsignal() -- get the signal action for a given signalpause() -- wait until a signal arrives [Unix only]default_int_handler() -- default SIGINT handlersignal constants:
SIG_DFL -- used to refer to the system default handlerSIG_IGN -- used to ignore the signalNSIG -- number of defined signalsSIGINT, SIGTERM, etc. -- signal numbersitimer constants:
ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon expirationITIMER_VIRTUAL -- decrements only when the process is executing, and delivers SIGVTALRM upon expirationITIMER_PROF -- decrements both when the process is executing and when the system is executing on behalf of the process. Coupled with ITIMER_VIRTUAL, this timer is usually used to profile the time spent by the application in user and kernel space. SIGPROF is delivered upon expiration.*** IMPORTANT NOTICE ***
A signal handler function is called with two arguments: the first is the signal number, the second is the interrupted stack frame.
alarm(seconds, /) — Arrange for SIGALRM to arrive after the given number of seconds.default_int_handler(signalnum, frame, /) — The default handler for SIGINT installed by Python. It raises KeyboardInterrupt.getitimer(which, /) — Returns current value of given itimer.getsignal(signalnum, /) — Return the current action for the given signal. The return value can be: SIG_IGN if the signal is being ignored, SIG_DFL if the default action is in effect, None if an unknown handler is in effect, anything else the callable Python object used as a handler.pause() — Wait until a signal arrives.pidfd_send_signal(pidfd, signalnum, siginfo=None, flags=0, /) — Send a signal to a process referred to by a pid file descriptor.pthread_kill(thread_id, signalnum, /) — Send a signal to a thread.pthread_sigmask(how, mask, /) — Fetch and/or change the signal mask of the calling thread.raise_signal(signalnum, /) — Send a signal to the executing process.set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd — Sets the fd to be written to (with the signal number) when a signal comes in. A library can use this to wakeup select or poll. The previous fd or -1 is returned. The fd must be non-blocking.setitimer(which, seconds, interval=0.0, /) — Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF). The timer will fire after value seconds and after that every interval seconds. The itimer can be cleared by setting seconds to zero. Returns old values as a tuple: (delay, interval).siginterrupt(signalnum, flag, /) — Change system call restart behaviour. If flag is False, system calls will be restarted when interrupted by signal sig, else system calls will be interrupted.signal(signalnum, handler, /) — Set the action for the given signal. The action can be SIG_DFL, SIG_IGN, or a callable Python object. The previous action is returned. See getsignal() for possible return values. *** IMPORTANT NOTICE *** A signal handler function is called with two arguments: the first is the signal number, the second is the interrupted stack frame.sigpending() — Examine pending signals. Returns a set of signal numbers that are pending for delivery to the calling thread.sigtimedwait(sigset, timeout, /) — Like sigwaitinfo(), but with a timeout. The timeout is specified in seconds, with floating point numbers allowed.sigwait(sigset, /) — Wait for a signal. Suspend execution of the calling thread until the delivery of one of the signals specified in the signal set sigset. The function accepts the signal and returns the signal number.sigwaitinfo(sigset, /) — Wait synchronously until one of the signals in *sigset* is delivered. Returns a struct_siginfo containing information about the signal.strsignal(signalnum, /) — Return the system description of the given signal. The return values can be such as "Interrupt", "Segmentation fault", etc. Returns None if the signal is not recognized.valid_signals() — Return a set of valid signal numbers on this platform. The signal numbers returned by this function can be safely passed to functions like pthread_sigmask.ITIMER_PROF = 2ITIMER_REAL = 0ITIMER_VIRTUAL = 1NSIG = 65SIGABRT = 6SIGALRM = 14SIGBUS = 7SIGCHLD = 17SIGCLD = 17SIGCONT = 18SIGFPE = 8SIGHUP = 1SIGILL = 4SIGINT = 2SIGIO = 29SIGIOT = 6SIGKILL = 9SIGPIPE = 13SIGPOLL = 29SIGPROF = 27SIGPWR = 30SIGQUIT = 3SIGRTMAX = 64SIGRTMIN = 34SIGSEGV = 11SIGSTOP = 19SIGSYS = 31SIGTERM = 15SIGTRAP = 5SIGTSTP = 20SIGTTIN = 21SIGTTOU = 22SIGURG = 23SIGUSR1 = 10SIGUSR2 = 12SIGVTALRM = 26SIGWINCH = 28SIGXCPU = 24SIGXFSZ = 25SIG_BLOCK = 0SIG_DFL = 0SIG_IGN = 1SIG_SETMASK = 2SIG_UNBLOCK = 1(built-in)
Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-02 17:03 @216.73.216.239
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)