Skip to content

Commit 865dba1

Browse files
authored
Merge pull request #426 from CharlesPerrotMinotHCHB/patch-2
Switch from on_event to lifespan asynccontextmanager #422
2 parents 91ba6d7 + f203d23 commit 865dba1

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ or
5151
### Quick Start
5252

5353
```python
54+
from collections.abc import AsyncIterator
55+
from contextlib import asynccontextmanager
56+
5457
from fastapi import FastAPI
5558
from starlette.requests import Request
5659
from starlette.responses import Response
@@ -74,12 +77,11 @@ async def get_cache():
7477
async def index():
7578
return dict(hello="world")
7679

77-
78-
@app.on_event("startup")
79-
async def startup():
80+
@asynccontextmanager
81+
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
8082
redis = aioredis.from_url("redis://localhost")
8183
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
82-
84+
yield
8385
```
8486

8587
### Initialization

0 commit comments

Comments
 (0)