A middleware for FastAPI that sends early responses from route handlers.
Project description
FastAPI Yieldable Response
A middleware for FastAPI that sends early responses from route handlers.
Overview
FastAPI Yieldable Response allows you to send immediate responses to clients while continuing to execute background processing in your route handlers. This is particularly useful when you want to:
- Send quick acknowledgments to clients before performing time-consuming operations
- Improve perceived response times by returning data immediately
- Execute cleanup or logging tasks after the response has been sent
Installation
pip install fastapi_yieldable_response
Quick Start
from fastapi import FastAPI
from fastapi_yieldable_response import yieldable_response
app = FastAPI()
@app.get("/")
@yieldable_response
async def root():
# This response is sent immediately to the client
yield {"message": "Hello!"}
# This code runs in the background after the response is sent
import time
time.sleep(2)
print("Background task completed!")
How It Works
The @yieldable_response decorator transforms your route handler into a function that:
- Immediate Response: The first
yieldstatement sends an immediate response to the client - Background Execution: Any code after the
yieldcontinues executing as a background task - Automatic Cleanup: Uses FastAPI's
BackgroundTasksto ensure proper resource management
Features
- Zero Dependencies: Only requires FastAPI (no additional dependencies)
- Async/Sync Support: Works with both async and sync route handlers
- Type Safety: Maintains original function signatures and type hints
- FastAPI Integration: Uses FastAPI's built-in
BackgroundTaskssystem - Simple API: Just add the decorator to your existing routes
Usage Examples
Async Route Handler
@app.get("/async-example")
@yieldable_response
async def async_example():
yield {"status": "processing", "id": 123}
# Background processing
await some_async_operation()
await send_notification()
Sync Route Handler
@app.get("/sync-example/{item_id}")
@yieldable_response
def sync_example(item_id: str):
yield {"message": f"Processing item {item_id}"}
# Background processing
process_item(item_id)
log_operation(item_id)
With Path Parameters
@app.get("/users/{user_id}")
@yieldable_response
async def get_user(user_id: int):
# Send immediate response
yield {"user_id": user_id, "status": "found"}
# Log access in background
await log_user_access(user_id)
await update_analytics(user_id)
Requirements
- Python 3.10+
- FastAPI 0.68.0+
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Repository
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_yieldable_response-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_yieldable_response-0.1.0.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5594e722cb27a5a01a622b411bfc04a68d0b5969f9e4142b787e3b5c39c00cfc
|
|
| MD5 |
3ff134c107dab613a4e690a761e409f8
|
|
| BLAKE2b-256 |
1862de84e991651b2ca5defb7c651d819e40dc8be041901240a3e99ac9e12fa5
|
File details
Details for the file fastapi_yieldable_response-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_yieldable_response-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aeb28dfd8e668af0451a1b9ff83ea2140e99773411b07f6772daea4a151e8c01
|
|
| MD5 |
641d5a0851629b46e2d7d708d26e9549
|
|
| BLAKE2b-256 |
717b97a2feb748678b6a646c3e11e3652b8898c0452b45402209ece0826a7b43
|