Application Lifecycle
Nexios Application Lifecycle
Introduction
Nexios provides multiple ways to manage application initialization and cleanup:
Decorators:
on_startup
: Functions to run at application startup.on_shutdown
: Functions to run at application shutdown.
Lifespan Async Context Manager: A single async context manager to handle both startup and shutdown logic.
Application Startup
The @app.on_startup()
decorator registers functions that should execute when the Nexios application starts. This is useful for initializing databases, setting up cache systems, or loading configuration settings.
Example: Initialize Database on Startup
Application Shutdown
The @app.on_shutdown()
decorator registers functions to execute when the application shuts down. This is useful for closing database connections, clearing cache, or performing cleanup tasks.
Example: Closing Database Connections on Shutdown
Lifespan Async Context Manager
Nexios also supports using an asynchronous context manager to handle both startup and shutdown events in a single construct. This is achieved by defining a lifespan function and passing it to the application via the lifespan
argument. This approach encapsulates initialization and cleanup logic in one place.
Example: Using Lifespan for Database Initialization and Cleanup
Key Notes:
The
lifespan
parameter accepts an async context manager that handles both startup (beforeyield
) and shutdown (afteryield
).Avoid combining this method with
@app.on_startup()
/@app.on_shutdown()
decorators for the same tasks to prevent redundant execution.
By leveraging on_startup
, on_shutdown
, or the lifespan
async context manager, developers can ensure resources are properly initialized and cleaned up, leading to stable and maintainable Nexios applications.
Last updated