]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-78530: add support for generators in `asyncio.wait` (#102761)
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Fri, 17 Mar 2023 01:28:43 +0000 (06:58 +0530)
committerGitHub <noreply@github.com>
Fri, 17 Mar 2023 01:28:43 +0000 (06:58 +0530)
Doc/library/asyncio-task.rst
Doc/whatsnew/3.12.rst
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS.d/next/Library/2023-03-16-16-43-04.gh-issue-78530.Lr8eq_.rst [new file with mode: 0644]

index 5f1449e1b599efba6dca258cdf5566307a1c65ec..a0900cd25a7731bbae3ea3685440a32fc49a8983 100644 (file)
@@ -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*
index a398cd93f73120dfa5f76239344909075f1edffc..b55b9619fac226736a968dd71c6558c1813ba237 100644 (file)
@@ -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
 -------
 
index 5b935b526541a1c2c9c425aec2365414229097e7..731fa0c5a60b9b97888fd7358b4b973a9580c0f2 100644 (file)
@@ -1373,6 +1373,22 @@ class BaseTaskTests:
         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():
diff --git a/Misc/NEWS.d/next/Library/2023-03-16-16-43-04.gh-issue-78530.Lr8eq_.rst b/Misc/NEWS.d/next/Library/2023-03-16-16-43-04.gh-issue-78530.Lr8eq_.rst
new file mode 100644 (file)
index 0000000..bdb46d0
--- /dev/null
@@ -0,0 +1 @@
+:func:`asyncio.wait` now accepts generators yielding tasks. Patch by Kumar Aditya.