From 284f353209e54c4e4da32ccc8c7670853d99e71c Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 28 Sep 2024 00:35:08 +0530 Subject: [PATCH 1/5] add back loop param --- Lib/asyncio/staggered.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/asyncio/staggered.py b/Lib/asyncio/staggered.py index 4458d01dece0e6..8c5b7f6bfa3c03 100644 --- a/Lib/asyncio/staggered.py +++ b/Lib/asyncio/staggered.py @@ -11,7 +11,7 @@ class _Done(Exception): pass -async def staggered_race(coro_fns, delay): +async def staggered_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 @@ -82,7 +82,11 @@ async def run_one_coro(this_index, coro_fn, this_failed): raise _Done try: - async with taskgroups.TaskGroup() as tg: + tg = taskgroups.TaskGroup() + # Intentionally override the loop in the TaskGroup to avoid + # using the running loop, preserving backwards compatibility + tg._loop = loop + async with tg: for this_index, coro_fn in enumerate(coro_fns): this_failed = locks.Event() exceptions.append(None) From 207b36e95c73b842f696175eae59e8dc8e15531e Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 28 Sep 2024 00:56:10 +0530 Subject: [PATCH 2/5] add comment --- Lib/asyncio/staggered.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/asyncio/staggered.py b/Lib/asyncio/staggered.py index 8c5b7f6bfa3c03..f7f49df98fba10 100644 --- a/Lib/asyncio/staggered.py +++ b/Lib/asyncio/staggered.py @@ -85,6 +85,8 @@ async def run_one_coro(this_index, coro_fn, this_failed): tg = taskgroups.TaskGroup() # Intentionally override the loop in the TaskGroup to avoid # using the running loop, preserving backwards compatibility + # TaskGroup only starts using `_loop` after `__aenter__` + # so overrding it here is safe. tg._loop = loop async with tg: for this_index, coro_fn in enumerate(coro_fns): From fa6a3d750f48f1a04fe526a2361056affba223b0 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 28 Sep 2024 00:57:02 +0530 Subject: [PATCH 3/5] fix typo --- Lib/asyncio/staggered.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/staggered.py b/Lib/asyncio/staggered.py index f7f49df98fba10..876fcbab40728d 100644 --- a/Lib/asyncio/staggered.py +++ b/Lib/asyncio/staggered.py @@ -86,7 +86,7 @@ async def run_one_coro(this_index, coro_fn, this_failed): # Intentionally override the loop in the TaskGroup to avoid # using the running loop, preserving backwards compatibility # TaskGroup only starts using `_loop` after `__aenter__` - # so overrding it here is safe. + # so overriding it here is safe.gi tg._loop = loop async with tg: for this_index, coro_fn in enumerate(coro_fns): From 00275658e8097673280681afa106adbab9586a77 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 28 Sep 2024 01:01:35 +0530 Subject: [PATCH 4/5] fix another typo :() --- Lib/asyncio/staggered.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/staggered.py b/Lib/asyncio/staggered.py index 876fcbab40728d..6ccf5c3c269ff0 100644 --- a/Lib/asyncio/staggered.py +++ b/Lib/asyncio/staggered.py @@ -86,7 +86,7 @@ async def run_one_coro(this_index, coro_fn, this_failed): # Intentionally override the loop in the TaskGroup to avoid # using the running loop, preserving backwards compatibility # TaskGroup only starts using `_loop` after `__aenter__` - # so overriding it here is safe.gi + # so overriding it here is safe. tg._loop = loop async with tg: for this_index, coro_fn in enumerate(coro_fns): From 690746cd635929006a8d1320c5cb224292e5cd2c Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 28 Sep 2024 08:55:55 +0530 Subject: [PATCH 5/5] add test --- Lib/test/test_asyncio/test_staggered.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Lib/test/test_asyncio/test_staggered.py b/Lib/test/test_asyncio/test_staggered.py index 21a39b3f911747..8cd98394aea8f8 100644 --- a/Lib/test/test_asyncio/test_staggered.py +++ b/Lib/test/test_asyncio/test_staggered.py @@ -121,6 +121,25 @@ async def coro(index): self.assertIsInstance(excs[0], ValueError) self.assertIsNone(excs[1]) + def test_loop_argument(self): + loop = asyncio.new_event_loop() + async def coro(): + self.assertEqual(loop, asyncio.get_running_loop()) + return 'coro' + + async def main(): + winner, index, excs = await staggered_race( + [coro], + delay=0.1, + loop=loop + ) + + self.assertEqual(winner, 'coro') + self.assertEqual(index, 0) + + loop.run_until_complete(main()) + loop.close() + if __name__ == "__main__": unittest.main()