Programmatic startup/shutdown of ASGI apps.
Low maintenance activity. Last release over 3 years ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install asgi-lifespanasgi-lifespan
Programmatically send startup/shutdown lifespan events into ASGI applications. When used in combination with an ASGI-capable HTTP client such as HTTPX, this allows mocking or testing ASGI applications without having to spin up an ASGI server.
Features
- Send lifespan events to an ASGI app using
LifespanManager. - Support for
asyncioandtrio. - Fully type-annotated.
- 100% test coverage.
Installation
pip install 'asgi-lifespan==2.*'
Usage
asgi-lifespan provides a LifespanManager to programmatically send ASGI lifespan events into an ASGI app. This can be used to programmatically startup/shutdown an ASGI app without having to spin up an ASGI server.
LifespanManager can run on either asyncio or trio, and will auto-detect the async library in use.
Basic usage
# example.py
from contextlib import asynccontextmanager
from asgi_lifespan import LifespanManager
from starlette.applications import Starlette
# Example lifespan-capable ASGI app. Any ASGI app that supports
# the lifespan protocol will do, e.g. FastAPI, Quart, Responder, ...
@asynccontextmanager
async def lifespan(app):
print("Starting up!")
yield
print("Shutting down!")
app = Starlette(lifespan=lifespan)
async def main():
async with LifespanManager(app) as manager:
print("We're in!")
# On asyncio:
import asyncio; asyncio.run(main())
# On trio:
# import trio; trio.run(main)