Asynchedging: Async Racing And Hedging
This package provides tiny asyncio utilities for:
- Race multiple calls, optionally with per-call start delays, and return the first acceptable result, cancelling the rest
- Hedge a single call by starting backups immediately or after delays, returning the first winner
Install
pip install asynchedging
Demo
Run the included demo (sleep-based simulations, no network required):
uv run python main.py
Example of using asynchedging with OpenAI
import asyncio
from typing import Any, Awaitable, Callable
from openai import AsyncOpenAI
from asynchedging import WinnerInfo, hedge, race
client = AsyncOpenAI()
async def call_openai(prompt: str, model: str) -> Awaitable[Any]:
return await client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
)
async def main() -> None:
# Race two OpenAI models. race(...) can accept a list of (awaitable, delay) pairs
result, winner_info = await race([
(call_openai("Why is the sky blue?", model="gpt-4o"), 0.0), # starts immediately but takes ~0.80s to finish
(call_openai("Why is the sky blue?", model="gpt-4o-mini"), 0.25), # starts at 0.25s but takes ~0.65s to finish (likely winner)
])
print(result) # OpenAI response object
print(
f"meta: index={winner_info.index} started={winner_info.started_count} elapsed={winner_info.elapsed_s:.3f}s"
)
# Race two OpenAI models without delays, first to finish wins and the rest are cancelled
result, winner_info = await race([
call_openai("Why is the sky blue?", model="gpt-4o"),
call_openai("Why is the sky blue?", model="gpt-4o-mini"),
])
# Hedge one OpenAI call (start backups if slow) — delays are required (can be zero)
# Each delay will become the start time for the corresponding backup call
factory = call_openai("Another question", model="gpt-4o-mini")
result, winner_info = await hedge(factory, delays_s=(0.25, 0.75))
asyncio.run(main())
Notes:
- Use
accept=to validate answers (e.g., non-empty content) and keep waiting if unacceptable - Use
total_timeout_s=to set a hard cap for the entire race/hedge - Pending tasks are cancelled when a winner is chosen
race(...)accepts either awaitables or (awaitable, delay) pairs,hedge(...)requiresdelays_sfor replicas (delays can be zero)
Metadata
Release files for asynchedging 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| asynchedging-1.1.2.tar.gz | 13.0 kB | Without provenance | Details |
Release files / asynchedging-1.1.2.tar.gz
| Download URL | asynchedging-1.1.2.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a465fbefa2a77ac95d0bbf614a05db2e05bfccf110f5a9a0156402c629c4c6a1
|
|
BLAKE2b-256 checksum How to use checksums |
6e30865719410183ba0c7f74f773e714147641a64f193f5089dc2ed4bc20e0ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for asynchedging 1.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.