---
source: https://iinventedlight.com/library/notes/python-signal.md
title: "Python signal - handle SIGINT, SIGTERM and alarms in a process"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, signal - Set handlers for asynchronous events; python/cpython (GitHub), Lib/signal.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: signal"
tags: [python, signal, sigint, sigterm, handlers, stdlib]
client: Librarian
sources:
  - id: S1
    title: "signal - Set handlers for asynchronous events"
    publisher: Python docs
    url: https://docs.python.org/3/library/signal.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/signal.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/signal.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python signal - handle SIGINT, SIGTERM and alarms in a process
**Answer:** `signal.signal` sets the handler for signal signalnum to the function handler. Python signal handlers are always executed in the main Python thread. Handler constants include `SIG_DFL` and `SIG_IGN`.

## Examples

```python
import signal, time

stop = False

def handle(signum, frame):
    global stop
    stop = True

signal.signal(signal.SIGINT, handle)
signal.signal(signal.SIGTERM, handle)
# signal.alarm(5)  # SIGALRM on Unix

while not stop:
    time.sleep(0.5)
```

## Rules

- A handler receives `(signum, frame)`.
- `SIG_IGN` ignores a signal; `SIG_DFL` restores the default.

## See also

- [python-socket](https://iinventedlight.com/library/notes/python-socket/)

## Claims

- id: C1
  claim: signal.signal installs a handler for a signal number.
  source: S1
  evidence: "Set the handler for signal signalnum to the function handler"

- id: C2
  claim: Handlers run in the main Python thread.
  source: S1
  evidence: "Python signal handlers are always executed in the main Python thread"

- id: C3
  claim: Lib/signal.py imports the _signal module.
  source: S2
  evidence: "from _signal import *"

## Sources

- [S1] signal - Set handlers for asynchronous events. Python docs. https://docs.python.org/3/library/signal.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/signal.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/signal.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, signal - Set handlers for asynchronous events; python/cpython (GitHub), Lib/signal.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
