[Python-checkins] Updated code example for asyncio.gather (GH-20604) (GH-26120)

orsenthil webhook-mailer at python.org
Fri May 14 02:07:35 EDT 2021


https://github.com/python/cpython/commit/f38296feaa3b4d6f215088b21fd2452a4f5c2e4c
commit: f38296feaa3b4d6f215088b21fd2452a4f5c2e4c
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: orsenthil <skumaran at gatech.edu>
date: 2021-05-13T23:07:31-07:00
summary:

Updated code example for asyncio.gather (GH-20604) (GH-26120)

The previous example did not fully showcase the interest of using gather.

Here the example showcases "the result is an aggregate list of returned values".
(cherry picked from commit 56b8ea65d28bd865e2363e10e9f1c2ca3433ffc2)

Co-authored-by: josephernest <nouvellecollection at gmail.com>

Co-authored-by: josephernest <nouvellecollection at gmail.com>

files:
M Doc/library/asyncio-task.rst

diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst
index 201678b0a3d57e..87133e4cab3074 100644
--- a/Doc/library/asyncio-task.rst
+++ b/Doc/library/asyncio-task.rst
@@ -360,32 +360,35 @@ Running Tasks Concurrently
       async def factorial(name, number):
           f = 1
           for i in range(2, number + 1):
-              print(f"Task {name}: Compute factorial({i})...")
+              print(f"Task {name}: Compute factorial({number}), currently i={i}...")
               await asyncio.sleep(1)
               f *= i
           print(f"Task {name}: factorial({number}) = {f}")
+          return f
 
       async def main():
           # Schedule three calls *concurrently*:
-          await asyncio.gather(
+          L = await asyncio.gather(
               factorial("A", 2),
               factorial("B", 3),
               factorial("C", 4),
           )
+          print(L)
 
       asyncio.run(main())
 
       # Expected output:
       #
-      #     Task A: Compute factorial(2)...
-      #     Task B: Compute factorial(2)...
-      #     Task C: Compute factorial(2)...
+      #     Task A: Compute factorial(2), currently i=2...
+      #     Task B: Compute factorial(3), currently i=2...
+      #     Task C: Compute factorial(4), currently i=2...
       #     Task A: factorial(2) = 2
-      #     Task B: Compute factorial(3)...
-      #     Task C: Compute factorial(3)...
+      #     Task B: Compute factorial(3), currently i=3...
+      #     Task C: Compute factorial(4), currently i=3...
       #     Task B: factorial(3) = 6
-      #     Task C: Compute factorial(4)...
+      #     Task C: Compute factorial(4), currently i=4...
       #     Task C: factorial(4) = 24
+      #     [2, 6, 24]
 
    .. note::
       If *return_exceptions* is False, cancelling gather() after it



More information about the Python-checkins mailing list