Simplify lifespan context manager for background task management
This commit is contained in:
+2
-8
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user