[Python-checkins] gh-104144: Optimize gather to finish eagerly when all futures complete eagerly (#104138)

kumaraditya303 webhook-mailer at python.org
Sat May 6 11:15:34 EDT 2023


https://github.com/python/cpython/commit/263abd333d18b8825cf6d68a5051818826dbffce
commit: 263abd333d18b8825cf6d68a5051818826dbffce
branch: main
author: Itamar Ostricher <itamarost at gmail.com>
committer: kumaraditya303 <59607654+kumaraditya303 at users.noreply.github.com>
date: 2023-05-06T15:15:27Z
summary:

gh-104144: Optimize gather to finish eagerly when all futures complete eagerly (#104138)

files:
A Misc/NEWS.d/next/Library/2023-05-03-16-50-24.gh-issue-104144.yNkjL8.rst
M Lib/asyncio/tasks.py

diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index aa5269ade19a..7eb647bd1298 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -813,6 +813,7 @@ def _done_callback(fut):
     children = []
     nfuts = 0
     nfinished = 0
+    done_futs = []
     loop = None
     outer = None  # bpo-46672
     for arg in coros_or_futures:
@@ -829,7 +830,10 @@ def _done_callback(fut):
 
             nfuts += 1
             arg_to_fut[arg] = fut
-            fut.add_done_callback(_done_callback)
+            if fut.done():
+                done_futs.append(fut)
+            else:
+                fut.add_done_callback(_done_callback)
 
         else:
             # There's a duplicate Future object in coros_or_futures.
@@ -838,6 +842,13 @@ def _done_callback(fut):
         children.append(fut)
 
     outer = _GatheringFuture(children, loop=loop)
+    # Run done callbacks after GatheringFuture created so any post-processing
+    # can be performed at this point
+    # optimization: in the special case that *all* futures finished eagerly,
+    # this will effectively complete the gather eagerly, with the last
+    # callback setting the result (or exception) on outer before returning it
+    for fut in done_futs:
+        _done_callback(fut)
     return outer
 
 
diff --git a/Misc/NEWS.d/next/Library/2023-05-03-16-50-24.gh-issue-104144.yNkjL8.rst b/Misc/NEWS.d/next/Library/2023-05-03-16-50-24.gh-issue-104144.yNkjL8.rst
new file mode 100644
index 000000000000..b975d48ed338
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-05-03-16-50-24.gh-issue-104144.yNkjL8.rst
@@ -0,0 +1,3 @@
+Optimize :func:`asyncio.gather` when using :func:`asyncio.eager_task_factory`
+to complete eagerly if all fututres completed eagerly.
+Avoid scheduling done callbacks for futures that complete eagerly.



More information about the Python-checkins mailing list