Skip to main content

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

pip install inflight

Usage

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 for performance comparison with and without inflight.

License

GPL-3.0-only

Download files

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

Source Distribution

inflight_py-1.0.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

inflight_py-1.0.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file inflight_py-1.0.0.tar.gz.

File metadata

  • Download URL: inflight_py-1.0.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for inflight_py-1.0.0.tar.gz
Algorithm Hash digest
SHA256 26cdd9ca9e9ca8659fb9c607ee8a15ba612080c077b9087c58a9f39b91b5c916
MD5 d4cbac8458e5222b801db06171dd6f4b
BLAKE2b-256 8006ecba90265e158f23dee0a68d36316d57ed88299344b945caca475387a8b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for inflight_py-1.0.0.tar.gz:

Publisher: ci.yml on ademmenh/inflight-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file inflight_py-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: inflight_py-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for inflight_py-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3bb1970e6161418da7ca9d36dbc20d4b8372ceb02497c37cd6308fc5f0f555b8
MD5 43390546b9d394f45a7f79afdd652ea0
BLAKE2b-256 a7261c8773778db95938422171fe7c80fc6ea6da529f48398e90507e35e9fee2

See more details on using hashes here.

Provenance

The following attestation bundles were made for inflight_py-1.0.0-py3-none-any.whl:

Publisher: ci.yml on ademmenh/inflight-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page