asgi-lifespan
Programmatic startup/shutdown of ASGI apps.
What it is and what it does
asgi-lifespan provides a LifespanManager context manager that sends ASGI lifespan events (startup and shutdown) to an ASGI application without requiring an HTTP server to be running. This is primarily useful for testing and mocking ASGI applications—you can combine it with an ASGI-capable HTTP client to send test requests into an app that has been properly initialized through its lifespan protocol.
The package automatically detects whether you are running under asyncio or trio and handles the async context accordingly. It supports configurable startup and shutdown timeouts, exposes lifespan state for use in request handling, and raises clear exceptions if the application does not support the lifespan protocol or if timeouts occur. The single runtime dependency is sniffio, keeping installation friction minimal.
Use it for:
- Write tests for ASGI applications that need lifespan setup and teardown without spinning up a test server.
- Mock ASGI applications in unit tests by triggering their startup and shutdown handlers programmatically.
- Access and verify lifespan state in an ASGI app during testing via the manager's state-aware app reference.
- Test ASGI middleware and lifespan event handlers in isolation without deploying to a running server.
- Verify that an ASGI app's startup and shutdown logic executes correctly before integration testing.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Programmatically trigger ASGI application startup and shutdown events without running a server, enabling testing and mocking of ASGI apps in isolation.
Yes, if you test ASGI applications. It is a small, stable, permissive-licensed tool with low install friction and no known vulnerabilities. The long gap since the last release (2023-03-28) is a minor concern for new Python versions, but the repository remains active and the package is marked production/stable. Verify compatibility with your Python version before relying on it for new projects.
Install
asgi-lifespan on PyPI
pip
pip install asgi-lifespanuv
uv add asgi-lifespanpoetry
poetry add asgi-lifespanInstalling asgi-lifespan
Before you install
Low friction: single pure-Python dependency (sniffio). Package is in production/stable status but aging—last release was 2023-03-28, over two years old. Repository remains active with recent commits (2026-02-11) and no archived status, though the long gap between releases may signal maintenance is minimal rather than active.
License in practice
MIT license is permissive with no restrictions on use, modification, or distribution in proprietary or open-source projects.
Quickstart
from asgi_lifespan import LifespanManager
async def main():
async with LifespanManager(app) as manager:
print("App started")
import asyncio
asyncio.run(main())
Requires an async runtime (asyncio or trio); the package auto-detects which one is in use via sniffio.
Verify before relying
- Whether the package works with ASGI applications beyond those mentioned in the description.
- Current compatibility with Python 3.12+ given the last release was 2023-03-28 and classifiers list only up to 3.11.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — sniffio |
| Maintenance | aging — 1,235 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 10,518,789/month — #1,448 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: asgi_lifespan-2.1.0-py3-none-any.whl
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
aiohttp-asgi-connectorProvides an AIOHTTP connector that lets you…
permissive · top 15,000 on PyPI
async-asgi-testclientProvides an async test client for ASGI web…
permissive · top 15,000 on PyPI
fastapi-lifespan-managerProvides a lifespan manager for FastAPI that…
permissive · top 15,000 on PyPI
mangumMangum adapts ASGI applications to run on AWS…
permissive · top 5,000 on PyPI
timing-asgiTiming-asgi is an ASGI middleware that…
unclear · top 15,000 on PyPI
opentelemetry-instrumentation-asgiProvides ASGI middleware for OpenTelemetry that…
permissive · top 1,000 on PyPI
asgi-correlation-idASGI middleware that reads or generates…
permissive · top 5,000 on PyPI
sse-starletteProvides Server-Sent Events streaming for…
permissive · top 1,000 on PyPI
a2wsgiConverts between WSGI and ASGI applications…
permissive · top 5,000 on PyPI
zope.processlifetimeProvides event interfaces and implementations…
unclear · top 15,000 on PyPI