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:
zx.quit.and_call— register callbacks that run when the process exits.zx.quit.and_intercept— append custom handlers to the uncaught-exception chain.zx.quit.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 zx.quit.and_call
Use zx.quit.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 zx.quit
@zx.quit.and_call
def close_db():
print("closing database connection")
@zx.quit.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 zx.quit.and_intercept
zx.quit.and_intercept lets you insert a custom function into the chain that runs before the original sys.excepthook.
import zx.quit
def notify_sentry(exc_type, exc_value, traceback):
print(f"reporting {exc_type.__name__}: {exc_value}")
zx.quit.and_intercept(notify_sentry)
[ref: #on]
Detecting file changes and signals with zx.quit.on
zx.quit.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 zx.quit
checker = zx.quit.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]
zx.quit.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 zx.quit
@zx.quit.and_call
def cleanup():
print("shutting down")
def log_uncaught(exc_type, exc_value, traceback):
print(f"uncaught {exc_type.__name__}: {exc_value}")
zx.quit.and_intercept(log_uncaught)
checker = zx.quit.on(
signal=signal.SIGUSR1,
errno=137,
poll=2.5,
)
while checker.sleep(60):
pass
Metadata
Release files for kuit 2.0.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 | Provenance | Actions |
|---|---|---|---|---|
| kuit-2.0.0.tar.gz | 13.9 kB | Without provenance | Details for kuit-2.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kuit-2.0.0-py3-none-any.whl | Python 3 | none | any | Details for kuit-2.0.0-py3-none-any.whl |
Total release size: 21.3 kB
Release files / kuit-2.0.0.tar.gz
| Download URL | kuit-2.0.0.tar.gz |
|---|---|
| Size | 13.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d4ab6ed0a5acc0dde63efda8ed7bb32fc2eeb6c24a468fb7676998af75c3bbb
|
|
BLAKE2b-256 checksum How to use checksums |
037b66f19b68e9b09bf273c3329306e45ad349f12aa5501f9b921dce2065d231
|
| 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-2.0.0-py3-none-any.whl
| Download URL | kuit-2.0.0-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b40d8da5c1548df374b4c50bde704bdb764c278ccf21371b2a81001622391c0a
|
|
BLAKE2b-256 checksum How to use checksums |
c18790320bba96fe48f42d9057881bd9bca450d15a4f2e53b00496bce32b7ae6
|
| 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}
|
Security for kuit 2.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.