From ae37327095e6c3971fe86098c2504dc759027ac7 Mon Sep 17 00:00:00 2001 From: Kenneth Date: Sat, 20 Jul 2024 21:47:57 +0100 Subject: [PATCH] fix: cancel timer when server shuts down --- server.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/server.py b/server.py index 9928dbd..1ed6393 100644 --- a/server.py +++ b/server.py @@ -1,16 +1,27 @@ import threading # from generate import generate +from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.responses import FileResponse from fastapi.staticfiles import StaticFiles -app = FastAPI() +# the index of the current audio track from 0 to 9 current_index = -1 +# the timer that periodically advances the current audio track +t = None + + +@asynccontextmanager +async def lifespan(app: FastAPI): + advance() + yield + if t: + t.cancel() def advance(): - global current_index + global current_index, t # if current_index == 0: # generate(offset=5) @@ -28,7 +39,7 @@ def advance(): t.start() -advance() +app = FastAPI(lifespan=lifespan) @app.get("/current.mp3")