Skip to content

Commit f203d23

Browse files
Switch from on_event to lifespan asynccontextmanager
on_event is now deprecated, and to be replaced with lifespan: https://fastapi.tiangolo.com/advanced/events/
1 parent 91ba6d7 commit f203d23

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)