Allows for the creation of lifespan dependencies - FastAPI dependencies that are only run once per app.
Project description
FastAPI Lifespan Dependencies
Allows for the creation of lifespan dependencies - FastAPI dependencies that are only run once per app, instead of once per path.
This library is inspired by fastapi-lifespan-manager
and by this suggestion on the FastAPI GitHub page.
Usage
To get started with lifespan dependencies, create a Lifespan
object and pass
it to the FastAPI
constructor:
from fastapi import FastAPI
from fastapi_lifespan_dependencies import Lifespan
lifespan = Lifespan()
app = FastAPI(lifespan=lifespan)
Any function that yield
s a value can be turned into a lifespan dependency
by using the Lifespan.register
decorator method
(both def
and async def
functions are supported):
from redis.asyncio import redis
@lifespan.register
async def get_db():
db = Redis(host="localhost", port=6379)
yield db
await db.aclose()
That's it!
You can now use fastapi.Depends
to access the value yield
ed by the lifespan dependency:
from typing import Annotated
from fastapi import Depends
@app.get("/example")
async def get_example(db: Annotated[Redis, Depends(get_db)]):
return await db.get('example')
@app.delete("/example")
async def delete_example(db: Annotated[Redis, Depends(get_db)]):
return await db.delete('example')
Usage with Other Dependencies
Similarly to normal FastAPI dependencies, lifespan dependencies can depend on each other:
from pydantic_settings import BaseSettings
class Config(BaseSettings):
redis_host: str
redis_port: int
@lifespan.register
def get_config():
yield Config()
@lifespan.register
async def get_db(config: Annotated[Config, Depends(get_config)]):
db = Redis(host=config.redis_host, port=config.redis_port)
yield db
await db.aclose()
Lifespan dependencies can also depend on normal dependencies,
as long as none of them require path-related dependencies
(like Query()
or Body()
).
When Are Lifespan Dependencies Run?
Every function decorated with Lifespan.register
will be run when the application
starts, up until a yield
statement is encountered.
Everything past the yield
will only be executed right before the application shuts down.
This means that, unlike normal FastAPI dependencies, using the same dependency in multiple path functions won't run the dependency multiple times, and every function will share the same instance of the dependency.
Examples
More examples can be found here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi_lifespan_dependencies-0.1.0.tar.gz
.
File metadata
- Download URL: fastapi_lifespan_dependencies-0.1.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Windows/11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d96f9ba7bda88be8a66e2eeaf74b2a09ec0c92a6f50c1a8decf97003c3654a39 |
|
MD5 | 9d14dcee05da98978c5de1439d66cede |
|
BLAKE2b-256 | 399e82b49818646086f1574d482d1914e70432592851029c524ccda6a1013695 |
File details
Details for the file fastapi_lifespan_dependencies-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_lifespan_dependencies-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Windows/11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d848fb48f3a578e13dcd76ff354223dd3e804721f7772b49336abccc95a6756 |
|
MD5 | 63f807ebcc569655fb0984c942641086 |
|
BLAKE2b-256 | a874b3bf617ae65c01e89f02441ab3fc50803a89a798532c63072af4176e2705 |