Race Async Tasks and Return the First Success
Race several async replicas, return the first successful result, cancel the losing tasks, and report a clear failure.
Code
from __future__ import annotations
import asyncio
from collections.abc import Awaitable, Iterable
from typing import TypeVar
T = TypeVar("T")
async def first_success(awaitables: Iterable[Awaitable[T]], *, timeout: float) -> T:
tasks = [asyncio.create_task(awaitable) for awaitable in awaitables]
if not tasks:
raise ValueError("at least one awaitable is required")
loop = asyncio.get_running_loop()
deadline = loop.time() + timeout
pending = set(tasks)
failures: list[BaseException] = []
try:
while pending:
remaining = deadline - loop.time()
if remaining <= 0:
raise TimeoutError("no replica succeeded before the deadline")
done, pending = await asyncio.wait(
pending,
timeout=remaining,
return_when=asyncio.FIRST_COMPLETED,
)
if not done:
raise TimeoutError("no replica succeeded before the deadline")
for task in done:
error = task.exception()
if error is None:
return task.result()
failures.append(error)
messages = "; ".join(str(error) for error in failures)
raise RuntimeError(f"all replicas failed: {messages}")
finally:
for task in pending:
task.cancel()
if pending:
await asyncio.gather(*pending, return_exceptions=True)
async def fetch(name: str, delay: float, *, fail: bool = False) -> str:
await asyncio.sleep(delay)
if fail:
raise ConnectionError(f"{name} unavailable")
return name
async def main() -> None:
result = await first_success(
[fetch("slow", 0.20), fetch("fast", 0.05), fetch("broken", 0.01, fail=True)],
timeout=1.0,
)
print(f"winner: {result}")
assert result == "fast"
cancelled = asyncio.Event()
async def cancellable() -> str:
try:
await asyncio.sleep(30)
except asyncio.CancelledError:
cancelled.set()
raise
return "never"
assert await first_success(
[fetch("quick", 0.01), cancellable()],
timeout=1.0,
) == "quick"
await asyncio.sleep(0)
assert cancelled.is_set()
try:
await first_success(
[fetch("a", 0.01, fail=True), fetch("b", 0.02, fail=True)],
timeout=1.0,
)
except RuntimeError as exc:
print(f"all failed: {exc}")
if __name__ == "__main__":
asyncio.run(main())
How the snippet works
Count the functions first_success, fetch, main and cancellable as the entire surface area of Race Async Tasks and Return the First Success.
The one repetition in this snippet is the loop while pending. Asynchronous boundaries use await, so failures surface as exceptions at the call site rather than being swallowed. annotations, Awaitable, Iterable, TypeVar are the only symbols borrowed from __future__ — the coupling is explicit and narrow. ValueError, TimeoutError, RuntimeError, ConnectionError and return appears at a raise inside an except handler — it fires only while that failure type is already being handled.
What to watch for
- Dependency check:
__future__andasyncio— not built-in, must be present in the project. - The guard
not tasksshort-circuits the flow with an earlyraisewhen its condition holds.
Applying it
The main signature reads def first_success(awaitables: Iterable[Awaitable[T]], *, timeout: float) -> T
Output lines are prefixed (winner:, all failed:), making each step’s effect easy to spot.
main sequences first_success, fetch and cancellable; executing the file runs that flow once.
Statements that matter
async def fetch(name: str, delay: float, *, fail: bool = False) -> str:
await asyncio.sleep(delay)
if fail:
raise ConnectionError(f"{name} unavailable")
return name
done, pending = await asyncio.wait(
pending,
timeout=remaining,
return_when=asyncio.FIRST_COMPLETED,
)
async def cancellable() -> str:
try:
await asyncio.sleep(30)
except asyncio.CancelledError:
cancelled.set()
raise
return "never"
Interactive Tools for This Topic
FAQ
- When is the exception raised?
- Inside an `except` handler — the `ValueError`, `TimeoutError`, `RuntimeError`, `ConnectionError` and `return` fires only while that failure type is already being handled, not on its own trigger.
- Do I need to install anything before running this?
- Yes: `__future__` and `asyncio` must be present in the project or the import fails at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.