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.add_hook — 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.add_hook

kuit.add_hook 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.add_hook(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.add_hook(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.2.tar.gz (14.6 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.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kuit-0.0.2.tar.gz
  • Upload date:
  • Size: 14.6 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.2.tar.gz
Algorithm Hash digest
SHA256 3a5a646d516ecc104860ae1ae7c3cdaee0baea62e78027b8f2eeab0f4dc22bd5
MD5 10912da3faf21c317ec592a4cea3f778
BLAKE2b-256 bcb47091dc7cf1ee62b71120fb7f4aae5c99bbaef36746e2967efcaed169c3e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kuit-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7c98ff44e28c5e1277d9676a5c88f976020f0fd54d5e4d066fa4755f632d9448
MD5 530e4819998d3d63d4d6c6e7f4b3047b
BLAKE2b-256 bb03edd86a0f3fe4982f298b418093c70fb280b53bc41a42cfe464149240e2db

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.0

2 files

0.0.3

2 files

This release

0.0.2 This release

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