Skip to content

Commit

Permalink
GH-78530: add support for generators in asyncio.wait (#102761)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 authored Mar 17, 2023
1 parent f33b33e commit 4f5774f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,10 @@ Waiting Primitives
.. versionchanged:: 3.11
Passing coroutine objects to ``wait()`` directly is forbidden.

.. versionchanged:: 3.12
Added support for generators yielding tasks.


.. function:: as_completed(aws, *, timeout=None)

Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ asyncio
:mod:`asyncio` does not support legacy generator-based coroutines.
(Contributed by Kumar Aditya in :gh:`102748`.)

* :func:`asyncio.wait` now accepts generators yielding tasks.
(Contributed by Kumar Aditya in :gh:`78530`.)

inspect
-------

Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,22 @@ async def foo():
self.assertEqual(res, 42)
self.assertAlmostEqual(0.15, loop.time())


def test_wait_generator(self):
async def func(a):
return a

loop = self.new_test_loop()

async def main():
tasks = (self.new_task(loop, func(i)) for i in range(10))
done, pending = await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
self.assertEqual(len(done), 10)
self.assertEqual(len(pending), 0)

loop.run_until_complete(main())


def test_as_completed(self):

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

0 comments on commit 4f5774f

Please sign in to comment.