coro_fns = <generator object start_connection.<locals>.<genexpr> at 0x7feba06d1be0>, delay = 0.3
asyncdefstaggered_race(coro_fns, delay, *, loop=None):
"""Run coroutines with staggered start times and take the first to finish.
This method takes an iterable of coroutine functions. The first one is
started immediately. From then on, whenever the immediately preceding one
fails (raises an exception), or when *delay* seconds has passed, the next
coroutine is started. This continues until one of the coroutines complete
successfully, in which case all others are cancelled, or until all
coroutines fail.
The coroutines provided should be well-behaved in the following way:
* They should only ``return``if completed successfully.
* They should always raise an exception if they did not complete
successfully. In particular, if they handle cancellation, they should
probably reraise, like this::
try:
# do workexcept asyncio.CancelledError:
# undo partially completed workraise
Args:
coro_fns: an iterable of coroutine functions, i.e. callables that
return a coroutine object when called. Use ``functools.partial``or
lambdas to pass arguments.
delay: amount of time, in seconds, between starting coroutines. If
``None``, the coroutines will run sequentially.
loop: the event loop to use.
Returns:
tuple*(winner_result, winner_index, exceptions)* where
-*winner_result*: the result of the winning coroutine, or``None``if no coroutines won.
-*winner_index*: the index of the winning coroutine in``coro_fns``, or``None``if no coroutines won. If the winning
coroutine may returnNone on success, *winner_index* can be used
to definitively determine whether any coroutine won.
-*exceptions*: list of exceptions returned by the coroutines.
``len(exceptions)``is equal to the number of coroutines actually
started, and the order is the same asin``coro_fns``. The winning
coroutine's entry is ``None``."""#TODO: when we have aiter() and anext(), allow async iterables in coro_fns.
loop = loop or events.get_running_loop()
enum_coro_fns =enumerate(coro_fns)
winner_result =None
winner_index =None
exceptions = []
running_tasks = []
asyncdefrun_one_coro(
> previous_failed: typing.Optional[locks.Event]) -> None:
E NameError: name 'typing' is not defined. Did you forget to import 'typing'?
coro_fns = <generator object start_connection.<locals>.<genexpr> at 0x7feba06d1be0>
delay = 0.3
enum_coro_fns = <enumerate object at 0x7feba06223e0>
exceptions = []
loop = <_UnixSelectorEventLoop running=False closed=False debug=False>
running_tasks = []
winner_index = None
winner_result = None
/usr/lib/python3.13/asyncio/staggered.py:73: NameError
Bug report
Bug description:
asyncio/staggered.pyis usingtyping.Optional:cpython/Lib/asyncio/staggered.py
Line 73 in 65de194
However, #114281 removed the
typingimport. This causes test failures inaiohappyeyeballs:CC @AA-Turner
CPython versions tested on:
3.13, CPython main branch
Operating systems tested on:
Linux
Linked PRs
async.staggered.staggered_race#119173async.staggered.staggered_race(GH-119173) #119206