diff options
author | josephernest <nouvellecollection@gmail.com> | 2021-05-14 06:06:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-14 06:06:26 (GMT) |
commit | 56b8ea65d28bd865e2363e10e9f1c2ca3433ffc2 (patch) | |
tree | 8f08a049a7c9ead11ec51536fed266462f5a6a8c /Doc/library/asyncio-task.rst | |
parent | 65d180d98348289db1592e8dd2cc6af4225573ea (diff) | |
download | cpython-56b8ea65d28bd865e2363e10e9f1c2ca3433ffc2.zip cpython-56b8ea65d28bd865e2363e10e9f1c2ca3433ffc2.tar.gz cpython-56b8ea65d28bd865e2363e10e9f1c2ca3433ffc2.tar.bz2 |
Updated code example for asyncio.gather (GH-20604)
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".
Diffstat (limited to 'Doc/library/asyncio-task.rst')
-rw-r--r-- | Doc/library/asyncio-task.rst | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 3f54ecb..69e965c 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -358,32 +358,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 |