Skip to main content

A best-in-class async request coalescing library for Python.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiocoalesce-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiocoalesce-0.1.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file aiocoalesce-0.1.0.tar.gz.

File metadata

  • Download URL: aiocoalesce-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for aiocoalesce-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8da5e7500c926e9f5ffd8cb28d9b4cd5662d094008bed12525166d44ed5239fd
MD5 9e1e5aefcd885c90d18d86ac5f651c0a
BLAKE2b-256 596bbaebd87c67aaa512a92a27fb10ae0c658eefbd10596f046d5ef5b2d7de06

See more details on using hashes here.

File details

Details for the file aiocoalesce-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aiocoalesce-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for aiocoalesce-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b39bb16cb38768befadfad3859725417bf90a539eaaa603cb8865fbbed40a06d
MD5 7cc6faeeb88ddfa380baa7a17a5f8214
BLAKE2b-256 a4bdf737a6c4bee6927c516d829a1d98b459281effb4a9d6712cbac90c2d6643

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page