gh-134173: optimize state transfer between concurrent.futures.Future and asyncio.Future (#134174)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
J. Nick Koston 2025-05-18 11:56:20 -04:00 committed by GitHub
parent f2de1e6861
commit 53da1e8c8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 148 additions and 14 deletions

View file

@ -351,22 +351,19 @@ def _set_concurrent_future_state(concurrent, source):
def _copy_future_state(source, dest):
"""Internal helper to copy state from another Future.
The other Future may be a concurrent.futures.Future.
The other Future must be a concurrent.futures.Future.
"""
assert source.done()
if dest.cancelled():
return
assert not dest.done()
if source.cancelled():
done, cancelled, result, exception = source._get_snapshot()
assert done
if cancelled:
dest.cancel()
elif exception is not None:
dest.set_exception(_convert_future_exc(exception))
else:
exception = source.exception()
if exception is not None:
dest.set_exception(_convert_future_exc(exception))
else:
result = source.result()
dest.set_result(result)
dest.set_result(result)
def _chain_future(source, destination):
"""Chain two futures so that when one completes, so does the other.