shenchucheng commited on
Commit
d343fed
1 Parent(s): 629e5d0

replace startup event by lifespan

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -12,6 +12,7 @@ import time
12
  import traceback
13
  import uuid
14
  from collections import deque
 
15
  from functools import partial
16
  from typing import Dict
17
 
@@ -246,7 +247,14 @@ class LLMAPIHandler:
246
  return JSONResponse({"valid": False, "message": str(e)})
247
 
248
 
249
- app = FastAPI()
 
 
 
 
 
 
 
250
 
251
  app.mount(
252
  "/storage",
@@ -303,13 +311,6 @@ async def clear_storage(ttl: float = 1800):
303
  await asyncio.sleep(60)
304
 
305
 
306
- @app.on_event("startup")
307
- async def startup_event():
308
- # 运行在服务启动后立即执行的任务
309
- loop = asyncio.get_running_loop()
310
- loop.create_task(clear_storage())
311
-
312
-
313
  def main():
314
  server_config = CONFIG.get("SERVER_UVICORN", {})
315
  uvicorn.run(app="__main__:app", **server_config)
 
12
  import traceback
13
  import uuid
14
  from collections import deque
15
+ from contextlib import asynccontextmanager
16
  from functools import partial
17
  from typing import Dict
18
 
 
247
  return JSONResponse({"valid": False, "message": str(e)})
248
 
249
 
250
+ @asynccontextmanager
251
+ async def lifespan(app: FastAPI):
252
+ loop = asyncio.get_running_loop()
253
+ loop.create_task(clear_storage())
254
+ yield
255
+
256
+
257
+ app = FastAPI(lifespan=lifespan)
258
 
259
  app.mount(
260
  "/storage",
 
311
  await asyncio.sleep(60)
312
 
313
 
 
 
 
 
 
 
 
314
  def main():
315
  server_config = CONFIG.get("SERVER_UVICORN", {})
316
  uvicorn.run(app="__main__:app", **server_config)