e2dew32 commited on
Commit
d033ed5
·
verified ·
1 Parent(s): b5485f9

Upload 12 files

Browse files
Files changed (2) hide show
  1. app.py +8 -4
  2. src/scheduler.py +3 -0
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import asyncio
2
  import logging
 
3
  from typing import List
4
 
5
  import aiohttp
@@ -18,12 +19,15 @@ logger = logging.getLogger("free_api")
18
  model_tester = ModelTester()
19
  scheduler = Scheduler(task_callback=lambda: model_tester.scan_all_models())
20
 
21
- fastapi_app = FastAPI(title="Free Model API")
22
 
23
-
24
- @fastapi_app.on_event("startup")
25
- async def startup_event():
26
  scheduler.start()
 
 
 
 
 
27
 
28
 
29
  @fastapi_app.get("/v1/models")
 
1
  import asyncio
2
  import logging
3
+ from contextlib import asynccontextmanager
4
  from typing import List
5
 
6
  import aiohttp
 
19
  model_tester = ModelTester()
20
  scheduler = Scheduler(task_callback=lambda: model_tester.scan_all_models())
21
 
 
22
 
23
+ @asynccontextmanager
24
+ async def lifespan(_app: FastAPI):
 
25
  scheduler.start()
26
+ yield
27
+ scheduler.stop()
28
+
29
+
30
+ fastapi_app = FastAPI(title="Free Model API", lifespan=lifespan)
31
 
32
 
33
  @fastapi_app.get("/v1/models")
src/scheduler.py CHANGED
@@ -41,6 +41,9 @@ class Scheduler:
41
  self._run_task()
42
 
43
  def _run_task(self):
 
 
 
44
  try:
45
  self.task_callback()
46
  except Exception as e:
 
41
  self._run_task()
42
 
43
  def _run_task(self):
44
+ threading.Thread(target=self._run_task_impl, daemon=True).start()
45
+
46
+ def _run_task_impl(self):
47
  try:
48
  self.task_callback()
49
  except Exception as e: