A SIGALRM-based watchdog that can handle multiple timeouts.
Project description
Multidog
A SIGALRM-based watchdog that can handle multiple timeouts.
➡️ Quick Links: Repository · Issues
Multidog works by keeping track of one or more timeouts and setting up an alarm signal that will cause the OS to terminate your process when the timeout occurs.
🌱 Status
Multidog has recently been extracted from another project of mine. It kind of works, but I still have to fix some issues.
Things that are not yet implemented:
- Timeouts are only checked with the granularity of the shortest timeout. See the example below for what this means.
- You may only use one running instance of Multidog per process, because a process can only have one handler per signal. There is no safeguard against this at the moment.
- Adding more timeouts after creating the
Multidoginstance is not possible yet.
📚 Usage
Example usage:
from time import sleep
from multidog import Multidog
dog = Multidog({"a": 5, "b": 10})
dog.start()
# This will call `sys.exit()` in 5 seconds unless you reset the timeouts regularly.
sleep(4)
dog.reset("a")
# `sys.exit()` will be deferred for another 5 seconds.
sleep(4)
dog.reset("a")
# `sys.exit()` will be deferred for another 5 seconds.
# This is a bug actually: The "b" timeout has never been reset and only has two
# seconds left. Multidog should defer for only 2 seconds, but right now it doesn't.
sleep(4)
dog.reset("a")
# Multidog finally notices that "b" is overdue and will refuse to reset the alarm,
# causing your application to exit in one second.
dog.stop()
# Stops the watchdog before it kills your app.
Anonymous timeout
If you only have a single timeout to track, you can simplify your usage:
dog = Multidog(5) # equivalent to Multidog({"": 5})
dog.start()
dog.reset() # equivalent to dog.reset("")
Exit timeout
Your Python application might not shut down fast enough (or at all) on sys.exit() (because yeah, that's something that can be suppressed).
Therefore, Multidog will set up a second SIGALRM after calling sys.exit(), with the signal handler reset to the default, which should kill your process when the timeout expires.
The default timeout for this is 5 seconds, you can choose a different one like so:
dog = Multidog({"a": 5, "b": 10}, exit_timeout=120)
🗃️ Installation
Simply install the multidog package from PyPI via your preferred package manager.
For example, to add it as a dependency to your uv-managed project, use this:
uv add multidog
🧑💻 Development
Preparation
We're using uv to manage this project and its dependencies.
After cloning the repository using Git, a simple uv sync should get everything you need.
git clone https://codeberg.org/scy/multidog.git multidog
cd multidog
uv sync
direnv
We recommend using direnv to add installed dependencies to your $PATH, so that you don't need to prepend uv run to every command.
For example, after installing direnv, you can use direnv edit in this repository's directory and add the following line:
PATH_add .venv/bin
The rest of this readme assumes that you have .venv/bin in your $PATH.
EditorConfig
Make sure your editor or IDE supports the EditorConfig standard, so that your code adheres to the project's preferred indentation, line lengths, etc.
Makefile
There is a Makefile that contains frequently used commands to speed up development, but it's optional to use.
The following targets exist:
fmt: Use Ruff to format and lint the code.qa: Use mypy for static type analysis.reuse: Usereuse lintto make sure every file contains licensing information.test: Use pytest for automated testing and code coverage.noqa: Addnoqastatements to ignore everything Ruff complains about. Only use this after fixing everything that needs fixing.
make all, or simply make, will run fmt, qa, reuse, and test.
You should do this before every commit and fix all issues that are reported.
📃 License
This project is licensed under the terms of the MIT License.
The project also conforms to the REUSE Specification, version 3.3.
You can use the reuse tool to interpret the machine-readable licensing information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file multidog-0.1.0a1.tar.gz.
File metadata
- Download URL: multidog-0.1.0a1.tar.gz
- Upload date:
- Size: 41.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc8cbdfdea89f807939ea9d747da093390a4a1c90d65dfe77f77dea49de36d66
|
|
| MD5 |
23ca2295e8fbe0855b47d0c532756f72
|
|
| BLAKE2b-256 |
58a8c84848861cca592edfd0717e0c36e17fef98bff01e3e59fa6737f8e654ca
|
File details
Details for the file multidog-0.1.0a1-py3-none-any.whl.
File metadata
- Download URL: multidog-0.1.0a1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ce3c05a4a94d808ebd00d891b54f046e32364a78db3c9803048ca6dff830510
|
|
| MD5 |
907a3ab851c2c4bb604da634c6a9bcf2
|
|
| BLAKE2b-256 |
9afb637e84fa587130c8ddc383e3164d7692b0f9ea70cce75b704d7c6f5efaf9
|