Simplify lifespan context manager for background task management

This commit is contained in:
vvzvlad
2025-02-05 23:10:34 +03:00
parent 83a91d89ae
commit ce8fad8586
+2 -8
View File
@@ -33,19 +33,13 @@ Config = get_settings()
@asynccontextmanager
async def lifespan(_: FastAPI):
await client.start()
# Start background task
background_task = asyncio.create_task(cache_media_files())
background_task = asyncio.create_task(cache_media_files()) # Start background task
yield
# Cleanup
background_task.cancel()
background_task.cancel() # Cleanup
try:
await background_task
except asyncio.CancelledError:
pass
await client.stop()
app = FastAPI( title="Pyrogram Bridge", lifespan=lifespan)