skillfed

asgi-lifespan

Programmatic startup/shutdown of ASGI apps.

asgi-lifespan v2.1.0 10.5M downloads/30d#1,448 on PyPI277
Permissive license MIT AGING released

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-lifespan

uv

uv add asgi-lifespan

poetry

poetry add asgi-lifespan

Installing 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

Development Status :: 5 - Production/StableFramework :: AsyncIOFramework :: TrioIntended Audience :: DevelopersProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

asgi lifespan testingasgi app startup shutdownmock asgi applicationasgi lifespan eventstest asgi without serverasgi context managerlifespan protocol handler
asgi-testingasync-context-manager

More Testing packages

pluggy

Pluggy provides a plugin system that lets you…

permissive · top 100 on PyPI

pytest

pytest is a testing framework that lets you…

permissive · top 100 on PyPI

virtualenv

virtualenv creates isolated Python environments…

permissive · top 100 on PyPI

coverage

Coverage.py measures which lines of Python code…

permissive · top 1,000 on PyPI

pytest-asyncio

pytest-asyncio is a pytest plugin that enables…

permissive · top 1,000 on PyPI

pytest-json-ctrf

A pytest plugin that generates test reports in…

permissive · top 1,000 on PyPI

aiohttp-asgi-connector

Provides an AIOHTTP connector that lets you…

permissive · top 15,000 on PyPI

async-asgi-testclient

Provides an async test client for ASGI web…

permissive · top 15,000 on PyPI

fastapi-lifespan-manager

Provides a lifespan manager for FastAPI that…

permissive · top 15,000 on PyPI

mangum

Mangum adapts ASGI applications to run on AWS…

permissive · top 5,000 on PyPI

timing-asgi

Timing-asgi is an ASGI middleware that…

unclear · top 15,000 on PyPI

opentelemetry-instrumentation-asgi

Provides ASGI middleware for OpenTelemetry that…

permissive · top 1,000 on PyPI

asgi-correlation-id

ASGI middleware that reads or generates…

permissive · top 5,000 on PyPI

sse-starlette

Provides Server-Sent Events streaming for…

permissive · top 1,000 on PyPI

a2wsgi

Converts between WSGI and ASGI applications…

permissive · top 5,000 on PyPI

zope.processlifetime

Provides event interfaces and implementations…

unclear · top 15,000 on PyPI