Metadata-Version: 2.5
Name: inflight-py
Version: 1.0.0
Summary: Deduplicate concurrent requests by query_key
Project-URL: Homepage, https://github.com/user/inflight-py
Project-URL: Issues, https://github.com/user/inflight-py/issues
License-Expression: GPL-3.0-only
License-File: LICENSE
Keywords: async,concurrent,dedup,inflight,query,request
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.11
Provides-Extra: bench
Requires-Dist: asyncpg; extra == 'bench'
Requires-Dist: valkey; extra == 'bench'
Provides-Extra: dev
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: pytest-asyncio; extra == 'dev'
Description-Content-Type: text/markdown

# inflight

Deduplicate concurrent async requests by query_key. When multiple identical coroutines are in-flight, only one executes — the rest wait for and receive the same result.

## Why

In high-concurrency environments, identical queries (same DB row, same cache key) accumulate while each one independently hits the database or cache. inflight collapses these into a single call.

## Install

```bash
pip install inflight
```

## Usage

```python
import json
from inflight import InFlight


class Repo:
    def __init__(self, db, cache):
        self.db = db
        self.cache = cache
        self._inflight = InFlight()

    async def get_user(self, user_id: int):
        cached = await self._inflight.execute(
            f"user:{user_id}:cache",
            lambda: self.cache.get(f"user:{user_id}"),
        )

        if cached:
            return json.loads(cached)

        result = await self._inflight.execute(
            f"user:{user_id}:db",
            lambda: self.db.execute(
                select(users).where(users.c.id == user_id)
            ),
        )

        await self.cache.set(f"user:{user_id}", json.dumps(result), ex=5)
        return result
```

## Benchmarks

See [benchmarks/README.md](./benchmarks/README.md) for performance comparison with and without inflight.

## License

GPL-3.0-only
