Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7e5ef0a

Browse filesBrowse files
authored
bpo-42140: Improve asyncio.wait function (GH-22938)
# Improve asyncio.wait function The original code creates the futures set two times. We can create this set before, avoiding the second creation. This new behaviour [breaks the aiokafka library](aio-libs/aiokafka#672), because it gives an iterator to that function, so the second iteration become empty. Automerge-Triggered-By: GH:1st1
1 parent a13b26c commit 7e5ef0a
Copy full SHA for 7e5ef0a

File tree

3 files changed

+29
-2
lines changed
Filter options

3 files changed

+29
-2
lines changed

‎Lib/asyncio/tasks.py

Copy file name to clipboardExpand all lines: Lib/asyncio/tasks.py
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,15 @@ async def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED):
400400
"and scheduled for removal in Python 3.10.",
401401
DeprecationWarning, stacklevel=2)
402402

403-
if any(coroutines.iscoroutine(f) for f in set(fs)):
403+
fs = set(fs)
404+
405+
if any(coroutines.iscoroutine(f) for f in fs):
404406
warnings.warn("The explicit passing of coroutine objects to "
405407
"asyncio.wait() is deprecated since Python 3.8, and "
406408
"scheduled for removal in Python 3.11.",
407409
DeprecationWarning, stacklevel=2)
408410

409-
fs = {ensure_future(f, loop=loop) for f in set(fs)}
411+
fs = {ensure_future(f, loop=loop) for f in fs}
410412

411413
return await _wait(fs, timeout, return_when, loop)
412414

‎Lib/test/test_asyncio/test_tasks.py

Copy file name to clipboardExpand all lines: Lib/test/test_asyncio/test_tasks.py
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,30 @@ def gen():
15481548
loop.advance_time(10)
15491549
loop.run_until_complete(asyncio.wait([a, b]))
15501550

1551+
def test_wait_with_iterator_of_tasks(self):
1552+
1553+
def gen():
1554+
when = yield
1555+
self.assertAlmostEqual(0.1, when)
1556+
when = yield 0
1557+
self.assertAlmostEqual(0.15, when)
1558+
yield 0.15
1559+
1560+
loop = self.new_test_loop(gen)
1561+
1562+
a = self.new_task(loop, asyncio.sleep(0.1))
1563+
b = self.new_task(loop, asyncio.sleep(0.15))
1564+
1565+
async def foo():
1566+
done, pending = await asyncio.wait(iter([b, a]))
1567+
self.assertEqual(done, set([a, b]))
1568+
self.assertEqual(pending, set())
1569+
return 42
1570+
1571+
res = loop.run_until_complete(self.new_task(loop, foo()))
1572+
self.assertEqual(res, 42)
1573+
self.assertAlmostEqual(0.15, loop.time())
1574+
15511575
def test_as_completed(self):
15521576

15531577
def gen():
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve asyncio.wait function to create the futures set just one time.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.