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.and_call— register callbacks that run when the process exits.kuit.and_intercept— 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: #and-call]
Registering shutdown callbacks with kuit.and_call
Use kuit.and_call 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.and_call
def close_db():
print("closing database connection")
@kuit.and_call
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: #and-intercept]
Adding exception hooks with kuit.and_intercept
kuit.and_intercept 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.and_intercept(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.and_call
def cleanup():
print("shutting down")
def log_uncaught(exc_type, exc_value, traceback):
print(f"uncaught {exc_type.__name__}: {exc_value}")
kuit.and_intercept(log_uncaught)
checker = kuit.on(
signal=signal.SIGUSR1,
errno=137,
poll=2.5,
)
while checker.sleep(60):
pass
Metadata
Release files for kuit 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| kuit-1.1.0.tar.gz | 15.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kuit-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.7 kB
Release files / kuit-1.1.0.tar.gz
| Download URL | kuit-1.1.0.tar.gz |
|---|---|
| Size | 15.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
17fc8cff93327959cdb958cf7140ffceba1d15eea0d79b42312a63734e2a8c82
|
|
BLAKE2b-256 checksum How to use checksums |
437ad4f328c0df7e12ab40eccf18148dcf76a477b4198b1da5adc50788c47bf7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","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}
|
Release files / kuit-1.1.0-py3-none-any.whl
| Download URL | kuit-1.1.0-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
35c9746938fd58879658eeb97e3f576391ac6e9b3addc2ef3fab52534aa2d064
|
|
BLAKE2b-256 checksum How to use checksums |
139b801d1a38c057dd9a1107dbbbb8c8902b676dbf5bacc231894078f36d4477
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","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}
|