MCPcopy Index your code
hub / github.com/python/cpython / staggered_race

Function staggered_race

Lib/asyncio/staggered.py:14–178  ·  view source on GitHub ↗

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 n

(coro_fns, delay, *, loop=None)

Source from the content-addressed store, hash-verified

12
13
14async def staggered_race(coro_fns, delay, *, loop=None):
15 """Run coroutines with staggered start times and take the first to finish.
16
17 This method takes an iterable of coroutine functions. The first one is
18 started immediately. From then on, whenever the immediately preceding one
19 fails (raises an exception), or when *delay* seconds has passed, the next
20 coroutine is started. This continues until one of the coroutines complete
21 successfully, in which case all others are cancelled, or until all
22 coroutines fail.
23
24 The coroutines provided should be well-behaved in the following way:
25
26 * They should only ``return`` if completed successfully.
27
28 * They should always raise an exception if they did not complete
29 successfully. In particular, if they handle cancellation, they should
30 probably reraise, like this::
31
32 try:
33 # do work
34 except asyncio.CancelledError:
35 # undo partially completed work
36 raise
37
38 Args:
39 coro_fns: an iterable of coroutine functions, i.e. callables that
40 return a coroutine object when called. Use ``functools.partial`` or
41 lambdas to pass arguments.
42
43 delay: amount of time, in seconds, between starting coroutines. If
44 ``None``, the coroutines will run sequentially.
45
46 loop: the event loop to use.
47
48 Returns:
49 tuple *(winner_result, winner_index, exceptions)* where
50
51 - *winner_result*: the result of the winning coroutine, or ``None``
52 if no coroutines won.
53
54 - *winner_index*: the index of the winning coroutine in
55 ``coro_fns``, or ``None`` if no coroutines won. If the winning
56 coroutine may return None on success, *winner_index* can be used
57 to definitively determine whether any coroutine won.
58
59 - *exceptions*: list of exceptions returned by the coroutines.
60 ``len(exceptions)`` is equal to the number of coroutines actually
61 started, and the order is the same as in ``coro_fns``. The winning
62 coroutine's entry is ``None``.
63
64 """
65 loop = loop or events.get_running_loop()
66 parent_task = tasks.current_task(loop)
67 enum_coro_fns = enumerate(coro_fns)
68 winner_result = None
69 winner_index = None
70 unhandled_exceptions = []
71 exceptions = []

Callers 7

test_emptyMethod · 0.90
test_one_successfulMethod · 0.90
test_none_successfulMethod · 0.90
test_multiple_winnersMethod · 0.90
test_cancelledMethod · 0.90

Calls 10

setMethod · 0.95
enumerateFunction · 0.85
setFunction · 0.85
run_one_coroFunction · 0.85
EventMethod · 0.80
create_taskMethod · 0.45
addMethod · 0.45
add_done_callbackMethod · 0.45
create_futureMethod · 0.45
cancelMethod · 0.45

Tested by 7

test_emptyMethod · 0.72
test_one_successfulMethod · 0.72
test_none_successfulMethod · 0.72
test_multiple_winnersMethod · 0.72
test_cancelledMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…