8
8
from . import exceptions as exceptions_mod
9
9
from . import locks
10
10
from . import tasks
11
+ from . import futures
11
12
12
13
13
14
async def staggered_race (coro_fns , delay , * , loop = None ):
@@ -63,6 +64,7 @@ async def staggered_race(coro_fns, delay, *, loop=None):
63
64
"""
64
65
# TODO: when we have aiter() and anext(), allow async iterables in coro_fns.
65
66
loop = loop or events .get_running_loop ()
67
+ parent_task = tasks .current_task (loop )
66
68
enum_coro_fns = enumerate (coro_fns )
67
69
winner_result = None
68
70
winner_index = None
@@ -73,6 +75,7 @@ async def staggered_race(coro_fns, delay, *, loop=None):
73
75
74
76
def task_done (task ):
75
77
running_tasks .discard (task )
78
+ futures .future_discard_from_awaited_by (task , parent_task )
76
79
if (
77
80
on_completed_fut is not None
78
81
and not on_completed_fut .done ()
@@ -110,6 +113,7 @@ async def run_one_coro(ok_to_start, previous_failed) -> None:
110
113
this_failed = locks .Event ()
111
114
next_ok_to_start = locks .Event ()
112
115
next_task = loop .create_task (run_one_coro (next_ok_to_start , this_failed ))
116
+ futures .future_add_to_awaited_by (next_task , parent_task )
113
117
running_tasks .add (next_task )
114
118
next_task .add_done_callback (task_done )
115
119
# next_task has been appended to running_tasks so next_task is ok to
@@ -148,6 +152,7 @@ async def run_one_coro(ok_to_start, previous_failed) -> None:
148
152
try :
149
153
ok_to_start = locks .Event ()
150
154
first_task = loop .create_task (run_one_coro (ok_to_start , None ))
155
+ futures .future_add_to_awaited_by (first_task , parent_task )
151
156
running_tasks .add (first_task )
152
157
first_task .add_done_callback (task_done )
153
158
# first_task has been appended to running_tasks so first_task is ok to start.
@@ -171,4 +176,4 @@ async def run_one_coro(ok_to_start, previous_failed) -> None:
171
176
raise propagate_cancellation_error
172
177
return winner_result , winner_index , exceptions
173
178
finally :
174
- del exceptions , propagate_cancellation_error , unhandled_exceptions
179
+ del exceptions , propagate_cancellation_error , unhandled_exceptions , parent_task
0 commit comments