Skip to content

Commit 8e44125

Browse files
authored
refactor: Replace deprecated lifespan event (#4187)
1 parent 48a081e commit 8e44125

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

sdk/python/feast/feature_server.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import threading
44
import traceback
55
import warnings
6+
from contextlib import asynccontextmanager
67
from typing import List, Optional
78

89
import pandas as pd
@@ -50,15 +51,16 @@ def get_app(
5051
registry_ttl_sec: int = DEFAULT_FEATURE_SERVER_REGISTRY_TTL,
5152
):
5253
proto_json.patch()
53-
54-
app = FastAPI()
5554
# Asynchronously refresh registry, notifying shutdown and canceling the active timer if the app is shutting down
5655
registry_proto = None
5756
shutting_down = False
5857
active_timer: Optional[threading.Timer] = None
5958

60-
async def get_body(request: Request):
61-
return await request.body()
59+
def stop_refresh():
60+
nonlocal shutting_down
61+
shutting_down = True
62+
if active_timer:
63+
active_timer.cancel()
6264

6365
def async_refresh():
6466
store.refresh_registry()
@@ -70,14 +72,16 @@ def async_refresh():
7072
active_timer = threading.Timer(registry_ttl_sec, async_refresh)
7173
active_timer.start()
7274

73-
@app.on_event("shutdown")
74-
def shutdown_event():
75-
nonlocal shutting_down
76-
shutting_down = True
77-
if active_timer:
78-
active_timer.cancel()
75+
@asynccontextmanager
76+
async def lifespan(app: FastAPI):
77+
async_refresh()
78+
yield
79+
stop_refresh()
7980

80-
async_refresh()
81+
app = FastAPI(lifespan=lifespan)
82+
83+
async def get_body(request: Request):
84+
return await request.body()
8185

8286
@app.post("/get-online-features")
8387
def get_online_features(body=Depends(get_body)):

0 commit comments

Comments
 (0)