Skip to main content

title: kuit description: Graceful shutdown and file-change detection for Python

[ref: #kuit]

kuit

kuit is a tiny Python library for graceful process shutdown and development-time auto-restart.

It gives you three public entry points:

  • kuit.register — register callbacks that run when the process exits.
  • kuit.on_exception — append custom handlers to the uncaught-exception chain.
  • kuit.on — detect file changes or incoming signals and exit cleanly.

[ref: #installation]

Installation

Install with uv:

uv add kuit

Or with any PEP 517-compatible tool:

pip install kuit

[ref: #register]

Registering shutdown callbacks with kuit.register

Use kuit.register as a decorator or as a plain function call. The callback runs exactly once on SIGINT, SIGTERM, SIGQUIT, or an unhandled exception.

import kuit

@kuit.register
def close_db():
    print("closing database connection")

@kuit.register
def flush_logs():
    print("flushing logs")

Callbacks are executed in registration order. Exceptions raised by one callback are logged and do not stop the next callback from running.

[ref: #add-hook]

Adding exception hooks with kuit.on_exception

kuit.on_exception lets you insert a custom function into the chain that runs before the original sys.excepthook.

import kuit

def notify_sentry(exc_type, exc_value, traceback):
    print(f"reporting {exc_type.__name__}: {exc_value}")

kuit.on_exception(notify_sentry)

[ref: #on]

Detecting file changes and signals with kuit.on

kuit.on builds a callable that returns True while the application should keep running and False when it should exit. By default it watches the mtime of sys.argv[0]; if the file changes, it calls func(errno) and returns False.

import signal
import kuit

checker = kuit.on(signal=signal.SIGUSR1)

while checker.sleep(60):
    pass  # main loop work

checker() performs a single check immediately. checker.sleep(wait, poll=...) blocks for up to wait seconds, polling every poll seconds, and returns as soon as a change is detected or the deadline is reached.

[ref: #on-options]

kuit.on options

All arguments are keyword-only.

Option Type Default Description
func Callable[[int], Any] sys.exit Function called when the process should exit; receives errno.
signal int 0 POSIX signal number to listen for; set to 0 to disable signal handling.
errno int 137 Exit code passed to func on change detection.
sleep float 0.0 Default seconds to sleep inside checker() before returning.
poll float 2.5 Default polling interval used by checker.sleep().

[ref: #full-example]

Full example

import signal
import kuit

@kuit.register
def cleanup():
    print("shutting down")

def log_uncaught(exc_type, exc_value, traceback):
    print(f"uncaught {exc_type.__name__}: {exc_value}")

kuit.on_exception(log_uncaught)

checker = kuit.on(
    signal=signal.SIGUSR1,
    errno=137,
    poll=2.5,
)

while checker.sleep(60):
    pass

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kuit-1.0.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kuit-1.0.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file kuit-1.0.0.tar.gz.

File metadata

  • Download URL: kuit-1.0.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kuit-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5851e9c25e42cbfa3df9decb4bfa9b1abf67c5832cc9b3b22a97c52c33e3f07a
MD5 102b1b7becc7dc8606aea12ac7f0568b
BLAKE2b-256 f33f042d5eed7780b6642591808e66eb16a5972d075a2089893f2dd18f674751

See more details on using hashes here.

File details

Details for the file kuit-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: kuit-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kuit-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 686459eee87e3299349c3f17c3d42aeaa245349dfbcf539be56d2c3ec6c255a6
MD5 cdb5ce936bada3ab28dc5f4ce86d4e09
BLAKE2b-256 6f4afdcc03a663e8ec65b22c02ac66e7b027dbee14e5c7e959c1dffc8c47000a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page