Asyncio utilities for racing and hedged requests
Project description
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)
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
asynchedging-1.0.4.tar.gz
(12.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asynchedging-1.0.4.tar.gz.
File metadata
- Download URL: asynchedging-1.0.4.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4080539d24fda13fd993333999c8a8713fd42ea386c727011b7a5e2f2737764f
|
|
| MD5 |
8993c2c784dd8b45edf256b6f8c4b988
|
|
| BLAKE2b-256 |
43d3e83d3f41a384268e6678cd5e1cf5d7f553833fb2d175b341130d5c4d4b78
|
File details
Details for the file asynchedging-1.0.4-py3-none-any.whl.
File metadata
- Download URL: asynchedging-1.0.4-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d76ec5e406242923b78abea507e640de564d429467c5d4f5b87f55397786f85b
|
|
| MD5 |
c70ff1cc1500ff714db9c65447bb8cfd
|
|
| BLAKE2b-256 |
e6cf10fd32864d1f19c248b4d802ef4a717754ad4cb65c63f10b1eed6f1c3049
|