Handle signals and cleanup context
Project description
Aiorunner
Tiny wrapper around asyncio that starts long running tasks, installs
SIGINT/SIGTERM handlers, and guarantees a single cleanup point for your
application.
Highlights
- registers
SIGINTandSIGTERMhandlers soCtrl+Cor an orchestrator can stop the loop cleanly; - validates that the provided context function is an async generator with one
yield, otherwise raisesRuntimeError; - exposes
Runner.stop()so any coroutine can request shutdown; - forwards the
debugflag toasyncio.runfor richer diagnostics.
Installation
pip install aiorunner
Requires Python 3.11-3.14 (see pyproject.toml).
Quick start
import asyncio
from collections.abc import AsyncIterator
from aiorunner import Runner
async def app(runner: Runner) -> AsyncIterator[None]:
task = asyncio.create_task(asyncio.sleep(3600))
print("Ready")
yield # blocks until Ctrl+C or runner.stop()
task.cancel()
print("Bye")
Runner(app, debug=True).run()
Inside your context function:
- Prepare resources before
yield(servers, background tasks, DB pools). yieldonce. A secondyieldtriggersRuntimeError.- After
yield, perform cleanup. Callingrunner.stop()beforerun()or after shutdown raisesRuntimeErrorto help catch lifecycle bugs.
API surface
Runner(
context_function: ContextFunction,
*,
debug: bool = False,
**kwargs: Any,
)
context_functionmust beasync def context(runner, **kwargs) -> AsyncIterator[None].debugtogglesasyncio.run(..., debug=True).**kwargsare forwarded to the context function.
Methods:
run()starts the event loop and blocks until cleanup finishes.stop()can be scheduled from any task or thread via.
Observability
The package logger aiorunner logs DEBUG messages when entering the wait
loop, receiving a signal, and exiting cleanup. Configure logging handlers in
your app to surface these breadcrumbs.
Development
- Create/update the virtual environment with
uv sync --group dev; this keeps.venvaligned withuv.lock. - Automation lives in
mise.toml; runmise run lint,mise run basedpyright,mise run mypy,mise run test, or the umbrellamise run allto reuse the exact CI commands. - Hook runner: we lean on prek with the same
.pre-commit-config.yamlused in CI. Install it viauvx prek installand runuvx prek run --all(oruvx prek run basedpyright, etc.) before committing. - Additional helpers exist as
mise run format,mise run coverage,mise run build, andmise run uploadfor releases. toxtargetspy311,py312,py313, andlintso CI can mirror local runs.
License
MIT, see LICENSE.
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 aiorunner-0.2.5.tar.gz.
File metadata
- Download URL: aiorunner-0.2.5.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56e4fb940fc086fc7db5db8b7dcf50522a51e55710256c0c887f2efe01cebf2b
|
|
| MD5 |
b0379caefa26788715cd1d98b1f12ecf
|
|
| BLAKE2b-256 |
85a70706dbdfded7c5f2d1fa163e7a3b716925fa82099a80a8457df22e99fada
|
File details
Details for the file aiorunner-0.2.5-py3-none-any.whl.
File metadata
- Download URL: aiorunner-0.2.5-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c39357592a6429a21d8bdef2cbcb933a7b491ff27dcb2bf245b9a2dbaeff54f2
|
|
| MD5 |
14299431cc7a0ec278691464f594d3b6
|
|
| BLAKE2b-256 |
315c5d23dbbc1e19cb174e43389358b65a97ee24cfaef9ac8e98762cd78b36a1
|