File tree Expand file tree Collapse file tree 1 file changed +15
-11
lines changed
Expand file tree Collapse file tree 1 file changed +15
-11
lines changed Original file line number Diff line number Diff line change 33import threading
44import traceback
55import warnings
6+ from contextlib import asynccontextmanager
67from typing import List , Optional
78
89import 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 )):
You can’t perform that action at this time.
0 commit comments