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-0.0.3.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-0.0.3-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kuit-0.0.3.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-0.0.3.tar.gz
Algorithm Hash digest
SHA256 8a26b902e67af8495842103ac6f96e2c70563ef9915cacd8b3798486f9d1abaa
MD5 4ddb36380c7c721a6accc94f8ef8246c
BLAKE2b-256 201652200dc9865a07638171804013dc8884fa7524d937a059a324fb89e6e1fd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kuit-0.0.3-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-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 52f429b00458b8294e3bccdf170b2f2be761f870b69048a7633c42eafbe9579c
MD5 faeb9bd8d0fae88c7b3e6d8c7ed4dbe1
BLAKE2b-256 45b8046b9f4f2501539a32de66f36efdb233c148680fd64c8e7a29be5329b599

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.0

2 files

This release

0.0.3 This release

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