Installs signal handlers for common termination signals like SIGTERM and SIGINT to gracefully shut down applications. Reuse when managing lifecycle events for background daemons.
# Source: https://github.com/scrapy/scrapy | License: BSD-3-Clause | Copyright Scrapy developers
# Provided via justclaw.it.com; keep this notice with the code.
from __future__ import annotations
import signal
from collections.abc import Callable
from types import FrameType
from typing import Any, TypeAlias
# copy of _HANDLER from typeshed/stdlib/signal.pyi
SignalHandlerT: TypeAlias = (
Callable[[int, FrameType | None], Any] | int | signal.Handlers | None
)
signal_names: dict[int, str] = {member.value: member.name for member in signal.Signals}
def install_shutdown_handlers(
function: SignalHandlerT, override_sigint: bool = True
) -> None:
"""Install the given function as a signal handler for all common shutdown
signals (such as SIGINT, SIGTERM, etc). If ``override_sigi
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key