Startup and ShutdownΒΆ

A provisional addition to the ASGI specification adds the ability for coroutines to be awaited before a byte is received, startup and after the final byte is sent shutdown. This is particularly useful for creating and destroying connection pools. Quart provisionally supports this via before_serving() and after_serving() decorators which in the same way as before_first_request().

The decorated functions are called within the app context, allowing current_app and g to be used.

To use this functionality simply do the following,

@app.before_serving
async def create_db_pool():
    g.db_pool = await ...

@app.after_serving
async def create_db_pool():
    await g.db_pool.close()