Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adriangb/asgi-lifespan
Middleware to support ASGI lifespans as async context managers
https://github.com/adriangb/asgi-lifespan
Last synced: 21 days ago
JSON representation
Middleware to support ASGI lifespans as async context managers
- Host: GitHub
- URL: https://github.com/adriangb/asgi-lifespan
- Owner: adriangb
- License: mit
- Created: 2022-07-01T02:10:54.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-10-27T03:13:55.000Z (about 2 years ago)
- Last Synced: 2024-10-07T18:08:31.345Z (about 1 month ago)
- Language: Python
- Size: 47.9 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-asgi - asgi-lifespan-middleware - ASGI middlewate to support ASGI lifespans using a simple async context manager interface. (Resources / Experiments and examples)
README
# asgi-lifespan-middleware
ASGI middlewate to support ASGI lifespans using a simple async context manager interface.
This middleware accepts an ASGI application to wrap and an async context manager lifespan.
It will run both the lifespan it was handed directly and that of the ASGI app (if the wrapped ASGI app supports lifespans).## Example (Starlette)
Starlette apps already support lifespans so we'll just be using the TestClient against a plain ASGI app that does nothing.
```python
from contextlib import asynccontextmanager
from typing import AsyncIteratorfrom starlette.testclient import TestClient
from starlette.types import ASGIApp, Scope, Send, Receivefrom asgi_lifespan_middleware import LifespanMiddleware
@asynccontextmanager
async def lifespan(
# you'll get the wrapped app injected
app: ASGIApp,
) -> AsyncIterator[None]:
print("setup")
yield
print("teardown")async def app(scope: Scope, receive: Receive, send: Send) -> None:
... # do nothingwrapped_app = LifespanMiddleware(
app,
lifespan=lifespan,
)with TestClient(wrapped_app):
pass
```