FastAPI Utilities
raw JSON → 0.3.1 verified Mon Apr 27 auth: no python
A collection of reusable utilities for FastAPI including repeat, repeat_every, on_startup, and task decorators. Version 0.3.1 (requires Python >=3.7, <4.0). Release cadence is irregular.
pip install fastapi-utilities Common errors
error AttributeError: module 'fastapi_utilities' has no attribute 'repeat_every' ↓
cause Old import path used, or package not installed correctly.
fix
Run
pip install --upgrade fastapi-utilities and use from fastapi_utilities import repeat_every. error ImportError: cannot import name 'repeat_on_startup' from 'fastapi_utilities' ↓
cause Incorrect function name (does not exist).
fix
Use
from fastapi_utilities import on_startup. error TypeError: 'NoneType' object is not callable ↓
cause Decorator order reversed: `@repeat_every` placed above `@app.on_event`.
fix
Ensure
@app.on_event('startup') is the outermost decorator. Warnings
gotcha The decorator order matters when combining @app.on_event('startup') with @repeat_every: @app.on_event must be the outermost decorator. ↓
fix Apply @app.on_event on top, then @repeat_every.
deprecated The `repeat` decorator (without _every) is deprecated and may be removed in future versions. ↓
fix Use `repeat_every` or `repeat_at` instead.
gotcha Task functions must be async; the decorator does not support synchronous functions. ↓
fix Define the task with `async def`.
breaking In version 0.2.0, the package name was changed from `fastapi-utils` to `fastapi-utilities`. Install with the new name. ↓
fix Use `pip install fastapi-utilities`.
Imports
- repeat_every wrong
from fastapi_utilities.repeat import repeat_everycorrectfrom fastapi_utilities import repeat_every - repeat_at
from fastapi_utilities import repeat_at - on_startup
from fastapi_utilities import on_startup
Quickstart
from fastapi import FastAPI
from fastapi_utilities import repeat_every, on_startup
app = FastAPI()
@on_startup
async def startup():
print("App started")
@app.on_event("startup")
@repeat_every(seconds=30)
async def periodic_task():
print("Task executed")