Skip to content

Commit 4f5774f

Browse files
GH-78530: add support for generators in asyncio.wait (#102761)
1 parent f33b33e commit 4f5774f

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

Doc/library/asyncio-task.rst

+4
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,10 @@ Waiting Primitives
804804
.. versionchanged:: 3.11
805805
Passing coroutine objects to ``wait()`` directly is forbidden.
806806

807+
.. versionchanged:: 3.12
808+
Added support for generators yielding tasks.
809+
810+
807811
.. function:: as_completed(aws, *, timeout=None)
808812

809813
Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*

Doc/whatsnew/3.12.rst

+3
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ asyncio
241241
:mod:`asyncio` does not support legacy generator-based coroutines.
242242
(Contributed by Kumar Aditya in :gh:`102748`.)
243243

244+
* :func:`asyncio.wait` now accepts generators yielding tasks.
245+
(Contributed by Kumar Aditya in :gh:`78530`.)
246+
244247
inspect
245248
-------
246249

Lib/test/test_asyncio/test_tasks.py

+16
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,22 @@ async def foo():
13731373
self.assertEqual(res, 42)
13741374
self.assertAlmostEqual(0.15, loop.time())
13751375

1376+
1377+
def test_wait_generator(self):
1378+
async def func(a):
1379+
return a
1380+
1381+
loop = self.new_test_loop()
1382+
1383+
async def main():
1384+
tasks = (self.new_task(loop, func(i)) for i in range(10))
1385+
done, pending = await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
1386+
self.assertEqual(len(done), 10)
1387+
self.assertEqual(len(pending), 0)
1388+
1389+
loop.run_until_complete(main())
1390+
1391+
13761392
def test_as_completed(self):
13771393

13781394
def gen():
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`asyncio.wait` now accepts generators yielding tasks. Patch by Kumar Aditya.

0 commit comments

Comments
 (0)