aiocoalesce
A best-in-class async request coalescing library for Python (3.9+).
aiocoalesce ensures that multiple concurrent async requests for the same key share a single execution (the "SingleFlight" pattern).
This protects your backend from Thundering Herds without the complexity of Redis or caching layers.
Key Difference: Unlike other libraries, aiocoalesce is Cancellation Safe. If the first user disconnects, the shared work continues for other waiting users.
Installation
pip install aiocoalesce
Usage
import asyncio
from aiocoalesce import Coalescer
# Create a global coalescer
coalescer = Coalescer()
async def get_user_data(user_id: int):
# Simulate DB call
await asyncio.sleep(1)
return {"id": user_id, "name": "Alice"}
# In your API handler:
async def handler(user_id):
# If 10 requests come in at once for user_id=1,
# 'get_user_data' runs ONLY ONCE.
return await coalescer.run(user_id, get_user_data(user_id))
Why aiocoalesce?
| Feature | aiocoalesce |
Existing singleflight |
asyncio.Lock |
|---|---|---|---|
| Pattern | True Coalescing | True Coalescing | Sequential Locking |
| Cancellation | ✅ Shielded (Safe) | ❌ Cascading Fails | ✅ Safe |
| Exec Model | Concurrent | Concurrent | Sequential (Slow) |
Use Case
Perfect for:
- FastAPI endpoints with expensive DB queries.
- ML Inference servers (prevent running same model input twice).
- Proxy servers to reduce upstream load.
License
MIT
Release files for aiocoalesce 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aiocoalesce-0.1.0.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aiocoalesce-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.5 kB
Release files / aiocoalesce-0.1.0.tar.gz
| Download URL | aiocoalesce-0.1.0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8da5e7500c926e9f5ffd8cb28d9b4cd5662d094008bed12525166d44ed5239fd
|
|
BLAKE2b-256 checksum How to use checksums |
596bbaebd87c67aaa512a92a27fb10ae0c658eefbd10596f046d5ef5b2d7de06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / aiocoalesce-0.1.0-py3-none-any.whl
| Download URL | aiocoalesce-0.1.0-py3-none-any.whl |
|---|---|
| Size | 3.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b39bb16cb38768befadfad3859725417bf90a539eaaa603cb8865fbbed40a06d
|
|
BLAKE2b-256 checksum How to use checksums |
a4bdf737a6c4bee6927c516d829a1d98b459281effb4a9d6712cbac90c2d6643
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|