Skip to main content

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 SIGINT and SIGTERM handlers so Ctrl+C or an orchestrator can stop the loop cleanly;
  • validates that the provided context function is an async generator with one yield, otherwise raises RuntimeError;
  • exposes Runner.stop() so any coroutine can request shutdown;
  • forwards the debug flag to asyncio.run for 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:

  1. Prepare resources before yield (servers, background tasks, DB pools).
  2. yield once. A second yield triggers RuntimeError.
  3. After yield, perform cleanup. Calling runner.stop() before run() or after shutdown raises RuntimeError to help catch lifecycle bugs.

API surface

Runner(
    context_function: ContextFunction,
    *,
    debug: bool = False,
    **kwargs: Any,
)
  • context_function must be async def context(runner, **kwargs) -> AsyncIterator[None].
  • debug toggles asyncio.run(..., debug=True).
  • **kwargs are 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 .venv aligned with uv.lock.
  • Automation lives in mise.toml; run mise run lint, mise run basedpyright, mise run mypy, mise run test, or the umbrella mise run all to reuse the exact CI commands.
  • Hook runner: we lean on prek with the same .pre-commit-config.yaml used in CI. Install it via uvx prek install and run uvx prek run --all (or uvx prek run basedpyright, etc.) before committing.
  • Additional helpers exist as mise run format, mise run coverage, mise run build, and mise run upload for releases.
  • tox targets py311, py312, py313, and lint so CI can mirror local runs.

License

MIT, see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiorunner-0.2.5.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiorunner-0.2.5-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

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

Hashes for aiorunner-0.2.5.tar.gz
Algorithm Hash digest
SHA256 56e4fb940fc086fc7db5db8b7dcf50522a51e55710256c0c887f2efe01cebf2b
MD5 b0379caefa26788715cd1d98b1f12ecf
BLAKE2b-256 85a70706dbdfded7c5f2d1fa163e7a3b716925fa82099a80a8457df22e99fada

See more details on using hashes here.

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

Hashes for aiorunner-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c39357592a6429a21d8bdef2cbcb933a7b491ff27dcb2bf245b9a2dbaeff54f2
MD5 14299431cc7a0ec278691464f594d3b6
BLAKE2b-256 315c5d23dbbc1e19cb174e43389358b65a97ee24cfaef9ac8e98762cd78b36a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page