summaryrefslogtreecommitdiffstats
path: root/Doc/library/asyncio-task.rst
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2018-09-25 18:51:21 (GMT)
committerGitHub <noreply@github.com>2018-09-25 18:51:21 (GMT)
commit996859a90df51f84eab47351702cb59c6db4428a (patch)
treedc8133137515934b27a8be799eb392c945b3b271 /Doc/library/asyncio-task.rst
parent22ef31d0b4b497eda5e356528c3e1d29584d6757 (diff)
downloadcpython-996859a90df51f84eab47351702cb59c6db4428a.zip
cpython-996859a90df51f84eab47351702cb59c6db4428a.tar.gz
cpython-996859a90df51f84eab47351702cb59c6db4428a.tar.bz2
bpo-34790: [docs] Passing coroutines to asyncio.wait() can be confusing. (GH-9543)
Diffstat (limited to 'Doc/library/asyncio-task.rst')
-rw-r--r--Doc/library/asyncio-task.rst40
1 files changed, 36 insertions, 4 deletions
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst
index e995fb6..bb693d7 100644
--- a/Doc/library/asyncio-task.rst
+++ b/Doc/library/asyncio-task.rst
@@ -472,14 +472,20 @@ Waiting Primitives
return_when=ALL_COMPLETED)
Run :ref:`awaitable objects <asyncio-awaitables>` in the *aws*
- sequence concurrently and block until the condition specified
+ set concurrently and block until the condition specified
by *return_when*.
If any awaitable in *aws* is a coroutine, it is automatically
- scheduled as a Task.
+ scheduled as a Task. Passing coroutines objects to
+ ``wait()`` directly is deprecated as it leads to
+ :ref:`confusing behavior <asyncio_example_wait_coroutine>`.
Returns two sets of Tasks/Futures: ``(done, pending)``.
+ Usage::
+
+ done, pending = await asyncio.wait(aws)
+
The *loop* argument is deprecated and scheduled for removal
in Python 4.0.
@@ -514,9 +520,35 @@ Waiting Primitives
Unlike :func:`~asyncio.wait_for`, ``wait()`` does not cancel the
futures when a timeout occurs.
- Usage::
+ .. _asyncio_example_wait_coroutine:
+ .. note::
- done, pending = await asyncio.wait(aws)
+ ``wait()`` schedules coroutines as Tasks automatically and later
+ returns those implicitly created Task objects in ``(done, pending)``
+ sets. Therefore the following code won't work as expected::
+
+ async def foo():
+ return 42
+
+ coro = foo()
+ done, pending = await asyncio.wait({coro})
+
+ if coro in done:
+ # This branch will never be run!
+
+ Here is how the above snippet can be fixed::
+
+ async def foo():
+ return 42
+
+ task = asyncio.create_task(foo())
+ done, pending = await asyncio.wait({task})
+
+ if task in done:
+ # Everything will work as expected now.
+
+ Passing coroutine objects to ``wait()`` directly is
+ deprecated.
.. function:: as_completed(aws, \*, loop=None, timeout=None)